Skip to content
ohsudo, Home
Databases

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.

Beginner3 min readApplies to PostgreSQL 13–18 · psql · Docker · Docker Compose

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 app
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "app" does not exist

The 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

Terminal window
# Which databases exist?
psql -U app -d postgres -c '\l'
# Connect to one that exists
psql -U app -d shop
# Or create the one you need
createdb -U postgres -O app app

1. List the databases

Every server has a database called postgres. Connect to it to look around:

Terminal window
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:

Terminal window
psql -U app -d shop
CREATE DATABASE shop OWNER app;

CREATE 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 exist

Create it inside the container, as the POSTGRES_USER from your file, and start the app again:

Terminal window
docker compose exec db psql -U app -d app -c 'CREATE DATABASE shop'
Terminal window
docker compose up -d

Check 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 myshop finds 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 -h and -p, and on Debian or Ubuntu, pg_lsclusters lists 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 postgres to 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.

Open the terminal →

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles