PostgreSQL "FATAL: database does not exist": the name psql assumes, and the one you need
Fix PostgreSQL "FATAL: database does not exist": psql defaults to your user name, list databases, create the one you need, and handle Docker, case and connection strings.
by OhSudo ·
Checked on real software: PostgreSQL 18 · November 2026How we check
Not sure this is your cause?Find it step by step: Can't connect to the database →
Part of a learning path:SQL and PostgreSQL, step by step
$ psql -U apppsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "app" does not existThe server is running, and it accepted the login. Then it looked for the database you asked for, and there’s none by that name. Often you didn’t ask for one at all: without -d, psql and most PostgreSQL clients use a database with the same name as the user.
TL;DR
# Which databases exist?psql -U app -d postgres -c '\l'
# Connect to one that existspsql -U app -d shop
# Or create the one you needcreatedb -U postgres -O app app1. List the databases
Every server has a database called postgres. Connect to it to look around:
psql -U app -d postgres -c '\l'psql -l does the same. If the database you want isn’t in the list, it was never created on this server, or you’re connected to another server (section 4).
2. psql chose the name
psql -U app connects to the database app. Your application asked for one too, in its connection string, postgresql://app:…@db:5432/shop, where shop is the database. Either name it, or create a database with that name:
psql -U app -d shopCREATE DATABASE needs a superuser, or a role with CREATEDB. The environment variables PGDATABASE and PGUSER set the defaults if you’d rather not type them.
3. In Docker: POSTGRES_DB came too late
The official image creates POSTGRES_DB (or, without it, a database named like POSTGRES_USER) only the first time, when its volume is empty. Adding or changing POSTGRES_DB in compose.yaml later does nothing to the existing data. The app then fails:
psql: error: connection to server at "db" (172.18.0.2), port 5432 failed: FATAL: database "shop" does not existCreate it inside the container, as the POSTGRES_USER from your file, and start the app again:
docker compose up -dCheck the healthcheck too: pg_isready -d shop opens a real connection, so while shop is missing, the database’s log gets this FATAL line every few seconds, from the healthcheck itself.
Or, for a fresh setup with no data to keep, docker compose down -v deletes the volume, and the next start creates everything from the file. The same “only on the first start” rule causes password authentication failed after a password change. Practice both ways in the terminal, and get a file with all three variables set from the start from the Docker Compose Generator.
4. Other causes
- Capital letters: a database created as
CREATE DATABASE "MyShop"keeps its capitals, and-d myshopfinds nothing. Use the exact name, quoted in SQL. Lower-case names avoid the problem. - Another server: two PostgreSQL versions on one machine listen on different ports (5432, 5433), and a container may publish its port while a local server also runs. Check
-hand-p, and on Debian or Ubuntu,pg_lsclusterslists local clusters. - A typo in the connection string, such as a trailing slash, or a database name that picks up a query parameter.
- A restore into a database that doesn’t exist yet: create it first, or use
pg_restore --create -d postgresto have the dump create it.
To practice SQL on a database that’s ready, try the SQL Playground.
Shell, we have a problem!
Similar, but different: relation "x" does not exist means you’re in a database, and a table isn’t there. password authentication failed and role "x" does not exist fail at login, before the database is looked up. Connection refused means no server answered. See also the PostgreSQL Admin Cheat Sheet. The step before it, a missing role, is role “root” does not exist.
$ ./practice.sh
Practice this in the terminal
Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.