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.
Direct connection examples
Copy
Ask AI
const { Client } = require('pg')async function queryDatabase() { const databaseUrl = process.env.DATABASE_URL const client = new Client({ connectionString: databaseUrl }) try { await client.connect() const result = await client.query('SELECT * FROM users WHERE active = true') return result.rows } finally { await client.end() }}queryDatabase() .then(rows => console.log(rows)) .catch(err => console.error(err))
Pooled connection examples
Copy
Ask AI
const { Pool } = require('pg')async function queryDatabasePool() { const databaseUrl = process.env.DATABASE_URL // changes the URL to use the Neon's connection pooler const poolUrl = databaseUrl.replace('.us-east-2', '-pooler.us-east-2') const pool = new Pool({ connectionString: poolUrl, max: 10 }) try { const client = await pool.connect() try { const result = await client.query('SELECT * FROM users WHERE active = true') return result.rows } finally { client.release() } } finally { await pool.end() }}queryDatabasePool() .then(rows => console.log(rows)) .catch(err => console.error(err))
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.
If you need multiple Replit Apps to share a single database, you can expose it as a REST API. This approach allows other applications or services to securely access and manipulate database data through standardized HTTP requests.See the tutorial on sharing a database across multiple apps for a complete guide on setting up a secure database API service using the Database API Example template.