devnotes.

Installing PostgreSQL & Connecting

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl enable --now postgresql
sudo -u postgres psql          # log in as the superuser

The postgres OS user maps to the postgres database superuser through peer authentication, which is why sudo -u postgres works without a password.

macOS

brew install postgresql@17
brew services start postgresql@17
psql postgres

Postgres.app is a good alternative if you prefer a GUI installer.

Windows

Download the EDB installer from postgresql.org/download/windows, which bundles the server, pgAdmin and the Stack Builder. Set a password for the postgres superuser during setup and keep the default port 5432.

Docker

docker run --name pg17 \
  -e POSTGRES_PASSWORD=secret \
  -e POSTGRES_DB=practice \
  -p 5432:5432 -d postgres:17

docker exec -it pg17 psql -U postgres -d practice

Connecting with psql

psql -U postgres -d practice
psql -h localhost -p 5432 -U app -d practice
psql "postgresql://app:secret@localhost:5432/practice?sslmode=require"

The URI form is the same connection string your application will use.

Environment variables

export PGHOST=localhost PGPORT=5432 PGUSER=app PGDATABASE=practice
psql        # no flags needed now

Put passwords in ~/.pgpass (chmod 600) rather than in environment variables or command lines:

localhost:5432:practice:app:secret

Authentication: pg_hba.conf

Postgres decides how each connection authenticates using pg_hba.conf:

# TYPE  DATABASE  USER  ADDRESS         METHOD
local   all       all                   peer
host    all       all   127.0.0.1/32    scram-sha-256
host    practice  app   10.0.0.0/8      scram-sha-256

Rules are read top to bottom and the first match wins. Use scram-sha-256 (not md5, and never trust) for password authentication. Reload after editing:

sudo systemctl reload postgresql
SELECT pg_reload_conf();

GUI clients

  • pgAdmin 4 — the official web/desktop admin tool.
  • DBeaver / TablePlus / DataGrip — general-purpose multi-database clients.
  • pgcli — a psql replacement with autocomplete and syntax highlighting.
Install PostgreSQL on Linux, Windows, macOS and Docker | DevNotes