The database connection page contains the host, port, username, database name, and connection string for your PostgreSQL instance.
Connection string
A PostgreSQL connection string usually looks like this:
postgresql://<user>:<password>@<host>:5432/<database>?sslmode=require
Set it as an environment variable in your application:
export DATABASE_URL="postgresql://<user>:<password>@<host>:5432/<database>?sslmode=require"
Test with psql
Run a simple query:
TLS
Use TLS for application connections. If your framework supports sslmode=require, keep it enabled in production.
postgresql://<user>:<password>@<host>:5432/<database>?sslmode=require
Application examples
import pg from "pg";
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
});
require "pg"
conn = PG.connect(ENV.fetch("DATABASE_URL"))
puts conn.exec("select now()").first
import os
import psycopg
with psycopg.connect(os.environ["DATABASE_URL"]) as conn:
print(conn.execute("select now()").fetchone())
Treat database passwords like secrets. Do not commit connection strings to Git.