Connect your app to a SQL database
Learn how to connect to your Replit database from your Replit App.
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.
Create a directory for your connection script
Create a directory at the top level of your project called scripts
.
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.
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.
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:
Run the workflow
Select the arrow to the left of the workflow name to run it.
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: