GrepBoard/Manuals
605 manuals ·

Fix PostgreSQL FATAL: database does not exist (psql, Docker)

·Tested on PostgreSQL 17.11 (Docker postgres:17) · 2026-10·Test scope ↓·6 steps

PostgreSQL FATAL: database "appuser" does not exist means psql used the role name as the database. Pass -d with a real database or run createdb.

output
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "appuser" does not exist

If you don't name a database, PostgreSQL clients connect to the database with the same name as the role. When role appuser exists but database appuser doesn't, you get this error. The problem is the database name, not the role. Pass an existing database with -d, or create one with that name.

[1] List the databases. -l prints a table; the query below prints names only.

console
$ psql -U postgres -Atc 'SELECT datname FROM pg_database ORDER BY 1'postgrestemplate0template1

A fresh server has only these three. postgres is the default maintenance database, so any role can connect there first (if privileges allow).

console
$ psql -U appuser -d postgres -Atc 'SELECT current_user, current_database()'appuser|postgres

[2] Create the database you need. A role without the privilege can't run createdb. Create it as an admin role and make the app role its owner.

console
$ createdb -U appuser appusercreatedb: error: database creation failed: ERROR:  permission denied to create database$ createdb -U postgres -O appuser appuser && psql -U appuser -Atc "SELECT current_user, current_database()"appuser|appuser

Without -O appuser the owner is postgres. A database created on PostgreSQL 15 or later starts with no table creation in the public schema for ordinary roles (the database owner can), so without an extra grant appuser would connect but be unable to create tables. Databases upgraded from an older release or restored from a dump keep their original public privileges and ownership, so check the actual privileges there with \dn+ public. You could grant CREATEDB (ALTER ROLE appuser CREATEDB), but application accounts usually don't need it.

[3] Match the letter case. Unquoted names in SQL are folded to lower case. The real name of CREATE DATABASE MyApp is myapp, and the connection compares names exactly.

console
$ psql -U postgres -c 'CREATE DATABASE MyApp' && psql -U postgres -d MyAppCREATE DATABASEpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "MyApp" does not exist$ psql -U postgres -d myapp -Atc 'SELECT current_database()'myapp

[4] Check that the connection string has a database path. Leave off the trailing /name and the user name becomes the database name again. This is a common gap in application config.

console
$ psql 'postgresql://appuser@127.0.0.1:5432' -c 'SELECT 1'psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL:  database "appuser" does not exist$ psql 'postgresql://appuser@127.0.0.1:5432/myapp' -Atc 'SELECT current_database()'myapp

For command-line tools, set a default with PGDATABASE instead of typing it each time.

console
$ PGDATABASE=myapp psql -U appuser -Atc 'SELECT current_database()'myapp

[5] In Docker, check POSTGRES_DB. When set, the database gets that name and no database named after POSTGRES_USER is created. psql -U myapp without -d then goes to the missing myapp database.

console
$ docker exec db psql -U myapppsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "myapp" does not exist$ docker exec db psql -U myapp -d shop -Atc 'SELECT datname FROM pg_database ORDER BY 1'postgresshoptemplate0template1

Add -d shop, or make the application's database name match POSTGRES_DB. As with roles, changing POSTGRES_DB on a volume that already holds data creates nothing — run CREATE DATABASE yourself. Step [6] of Fix PostgreSQL role does not exist shows why.

[6] Check the server log for the name that was requested. Use this when the application wraps the error and hides the name.

output
FATAL:  database "appuser" does not existERROR:  permission denied to create databaseSTATEMENT:  CREATE DATABASE appuser;FATAL:  database "MyApp" does not exist

The quoted name on the FATAL line is what the client sent. If the same name repeats on a schedule, look at the connection settings of whatever runs at that time (cron jobs, health checks).

For role "…" does not exist, go to that guide; for password authentication failed, see this one.

Official references: psql — Connecting to a Database · libpq connection URIs · createdb · CREATE DATABASE · PostgreSQL 15 release notes — public schema privileges · Docker official image postgres — POSTGRES_DB

#postgresql#psql#createdb#docker