Postgres connect to database with URL

Serhii Shramko

Serhii Shramko /

2 min read--- views

Very often, cloud database services such as neon.tech allow you to connect via URL. That is, without credentials such as user, password, etc.

Connection details

Connection URL

It's official PostgreSQL format for connection URLs

S.Shramko personal site screenshot

Base URL and path

Here's an example of a base URL and path structure using uppercase placeholder values:

postgresql://USER:PASSWORD@HOST:PORT/DATABASE
S.Shramko personal site screenshot

How this site connects to the database

Store your credentials in .env

Store your credentials in your .env file.

PGHOST='<endpoint_hostname>:<port>'
PGDATABASE='<dbname>'
PGUSER='<username>'
PGPASSWORD='<password>'
ENDPOINT_ID='<endpoint_id>'

where:

  • endpoint_hostname the hostname of the branch endpoint. The endpoint hostname has an ep- prefix and appears similar to this: ep-tight-salad-272396.us-east-2.aws.neon.tech
  • dbname is the name of the database. The default Neon database is neondb
  • user is the database user
  • password is the database user's password, which is provided to you when you create a project
  • endpoint_id is the ID of the branch endpoint that you are connecting to. The endpoint_id has an ep- prefix and appears similar to this: ep-tight-salad-272396

Use prisma.io to connect Postgres

// You can create DATABASE_URL in your code
const DATABASE_URL = `postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}/${PGDATABASE}`;
# Or better to use .env file
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  referentialIntegrity = "prisma"
}

Use Datagrip to connect Postgres

You can use same credentials like HOST, PORT, USER, PASSWORD, DATABSE for connection to Database with DataGrip.

S.Shramko personal site screenshot

Share it: