PostgreSQL
What is PostgreSQL?
PostgreSQL is a powerful, open-source object-relational database system widely used in web applications and other software development projects. We use PostgreSQL version 15 in our integration, allowing you to easily create a production-ready PostgreSQL database directly within Replit. You can run queries and connect to the database using our provided environment variables. For more information on PostgreSQL, visit the official documentation.
Why use PostgreSQL?
- Simplicity: With our integration, you can easily set up a PostgreSQL database without having to install any additional software or configure any settings. All you need to do is click a button to have a fully-functional database ready to go.
- Seamless integration: Our integration is designed to work seamlessly with Replit, so you can easily access your database and run queries within the Replit environment or within your code with minimal configuration.
- Production-ready: The database created with our integration is production-ready, so you can use it for real-world applications and projects.
- Convenience: With our provided environment variables, you can easily connect to the database from your code, without having to worry about setting up the connection manually. This saves you time and makes it easier to get your project up and running.
- Neon documentation: Provides guides on how to connect various frameworks, such as Django, to your PostgreSQL database in the neon documentation.
- Connection pooling: Connection pooling reduces database overhead by reusing existing connections instead of creating new ones for each request, improving application performance and scalability. More information on connection pooling can be found in the neon documentation.
- Compute lifecycle: The database will sleep after 5 minutes without queries, so you may experience disconnects. More information on the compute lifecycle can be found in the Neon documentation
Setup
- Open a new tab in Replit and type "PostgreSQL"
- In the "PostgreSQL" panel, click "create a database"
- In the env section, you can view all of the relevant connection information about your database.
Billing
Being serverless, Replit PostgreSQL only charges for actual usage, resulting in potential cost savings of up to 10 times.
Learn more about Replit PostgreSQL usage based billing.
SQL Explorer
We provide a SQL explorer that you can use to create tables and manage your database. And if you have purchased Replit AI, you can use that within the SQL explorer to help you write queries.
Example Usage
The following examples assumes you created PostgreSQL database in Replit. This will ensure that the environment variables are set up correctly for you to connect to the database. You still need to create a users
table for the examples to work below.
JavaScript
- Basic connection example
// basic database connection
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 example
// using connection pooling
const { Pool } = require('pg')
async function queryDatabasePool() {
const databaseUrl = process.env.DATABASE_URL
// change 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))
Python
- Basic connection example
# basic database connection
import os
import psycopg2
database_url = os.environ['DATABASE_URL']
try:
conn = psycopg2.connect(database_url)
cur = conn.cursor()
# example query that assumes users table is present
cur.execute("SELECT * FROM users WHERE active = true")
results = cur.fetchall()
finally:
cur.close()
conn.close()
- Pooled connection example
# using connection pooling
import os
from psycopg2 import pool
database_url = os.environ['DATABASE_URL']
# change the URL to use the Neon's connection pooler
database_url = database_url.replace('.us-east-2', '-pooler.us-east-2')
connection_pool = pool.SimpleConnectionPool(1, 10, database_url)
try:
conn = connection_pool.getconn()
cur = conn.cursor()
# example query that assumes users table is present
cur.execute("SELECT * FROM users WHERE active = true")
results = cur.fetchall()
finally:
cur.close()
connection_pool.putconn(conn)
connection_pool.closeall()
Powered by Neon
Neon is a serverless PostgreSQL platform designed to help you build reliable and scalable applications faster. If you would like to access an existing database via Neon directly please reach out to support@replit.com.