PostgreSQL error: database does not exist
FATAL: database "gianluca" does not exist
You connected to the cluster successfully, but asked for a database that isn't there. When the "missing database" in the message is your own username, nothing is broken: psql simply defaults the database name to the user name when you don't specify one.
What this error means
A PostgreSQL server (cluster) hosts multiple databases; every connection targets exactly one. If the requested name doesn't exist on the cluster you actually reached, you get this FATAL. Both halves of that sentence cause confusion:
- The requested name may not be what you think — psql and many tools default it (to the user name), and environment variables like
PGDATABASEoverride quietly. - The cluster you actually reached may not be the one with your data: port 5432 vs 5433, host machine vs Docker container, local vs remote.
Common causes
- psql without
-d: database name defaults to the user name — the classicdatabase "gianluca" does not exist. - The database hasn't been created yet in this environment (fresh setup, new CI job, recreated container without its init scripts).
- Wrong cluster: connection string points at another port/host where that database genuinely doesn't exist.
- Case or typo: database names are identifiers — created quoted as
"AppDb", it must be requested with that exact case.
How to diagnose it
# Cosa c'è davvero su questo cluster:
psql -h HOST -p 5432 -U USER -d postgres -c '\l'
Connecting to the always-present postgres maintenance database and listing what exists answers both questions at once: whether you're on the right cluster, and what the database is actually called.
How to fix it
- Name the database explicitly:
psql -d appdb, or fix the connection string /PGDATABASE. - Create it if it's genuinely missing:
CREATE DATABASE appdb OWNER app_user; - Wrong cluster → fix host/port; on Debian-family systems
pg_lsclusterslists local clusters and their ports.
🔍
The twin error: role does not exist — same defaulting mechanism, on the user name side.
