This guide shows you how to connect to your Replit App’s database from your code using the connection methods:

  • Direct connection: Connection for development and lighter workloads
  • Connection pooling: Efficient connection management for high-traffic production applications

To determine which type of connection you need, see Choosing your driver and connection type in the Neon documentation.

Use Agent or Assistant to generate code that connects to your existing database.

Prerequisites

Before getting started, make sure you have the following:

  • Created a database in your Replit App
  • Knowledge of coding and database connection management

Create a connection script

This tutorial does not provide examples for all programming languages. Use PostgresSQL driver documentation for your project’s programming language or ask Assistant to translate the code examples.

1

Create a directory for your connection script

Create a directory at the top level of your project called scripts.

2

Create a connection script

Create a file in this directory and paste one of the following connection examples.

Create a workflow to run your script

Your workflow may vary depending on the language you chose and the file path of the script you created.

1

Add a new workflow

Navigate to the Workflows tool and select New Workflow to add a workflow. In the Workflow field, enter “test connection” as the name.

2

Create a command to run the script

Select Execute Shell Command under the Tasks heading. Add a command to run the script you created in the line below it.

The following screenshot shows the “test connection” workflow configured to run a JavaScript connection example:

3

Run the workflow

Select the arrow to the left of the workflow name to run it.

4

View the output

Navigate to the Console tool, where you should see a data from your users table, if any exists.

Next steps

To learn more about working with databases in Replit, see the following resources:

  • Database: learn how to create and manage a SQL database using the Replit Database tool.
  • Workflows: learn how to create and run custom workflows.