FATAL: database "..." does not exist means the server accepted the network connection and checked who you are, but there is no database with exactly that name in this cluster. Most of the time the client asked for the wrong name, and very often that name is simply your user name, because that is the default.
$ psql -h db1 -U app
psql: error: connection to server at "db1" (10.0.0.5), port 5432 failed:
FATAL: database "app" does not exist
The SQLSTATE is 3D000, invalid_catalog_name in the error codes table. The name in quotes is the one the server received. Start there: it tells you which part of your configuration is wrong.
Where the database name comes from
If you do not give a database name, libpq picks one for you. According to the libpq connection parameters, dbname "defaults to be the same as the user name". The psql documentation says the same: once the user name is determined, it is used as the default database name. So psql -U app quietly means "connect to database app". If you only ever created shop, you get exactly the error above.
The order in which a libpq-based client picks the name:
dbnamein the connection string or URI, or-don the command line;- the PGDATABASE environment variable;
- the user name, which itself defaults to your operating system user.
Drivers that do not use libpq (JDBC, many Node.js and Go drivers) have their own defaults and their own connection string formats, so check the driver documentation. The server side is always the same: it looks up the name it received.
Check what actually exists
Every cluster created by initdb has a database called postgres, plus template1 and template0, as the documentation on creating databases explains. Connect to postgres and look at pg_database:
-- connect to a database that surely exists, then list them all
$ psql -h db1 -U app -d postgres
postgres=> SELECT datname FROM pg_database ORDER BY 1;
-- or simply:
$ psql -h db1 -U app -d postgres -l
Then the fix is one of three things: point the client at the right name, set a default, or create the database the client expects:
# say which database you want
psql -h db1 -U app -d shop
psql "postgresql://app@db1:5432/shop"
# or set it once for the shell / service
export PGDATABASE=shop
# or create the database the tool expects
createdb -h db1 -U postgres -O app app
Note that the same "default to the user name" rule applies to roles. If the error says role "..." does not exist instead, that is a different problem, covered in our article on "role does not exist".
Upper case and quoted names
Unquoted SQL identifiers are folded to lower case; quoted ones keep their case, as the SQL syntax rules describe. A database created as "MyShop" in a GUI tool is really called MyShop, and asking for myshop fails:
CREATE DATABASE "MyShop"; -- quoted: the name is MyShop
CREATE DATABASE MyShop; -- unquoted: folded to myshop
$ psql -d myshop # FATAL: database "myshop" does not exist (first case)
$ psql -d MyShop # works for the quoted name
In our experience, this is common when databases are created with a GUI tool that quotes every name. The easy long-term fix is to use lower-case names without quotes. To rename an existing database, use ALTER DATABASE ... RENAME TO; you cannot rename the database you are connected to, and the server refuses while anyone else is connected to it, so plan a short window.
Docker: POSTGRES_DB is ignored the second time
The official postgres Docker image creates the database named in POSTGRES_DB (or, if that is not set, one named after POSTGRES_USER) when the container is first started. Its documentation warns that these variables only have an effect if the data directory is empty. Change POSTGRES_DB later, or reuse an old volume, and nothing new is created. The container starts fine, and the application fails with "database does not exist". Create the database by hand, or remove the volume if it really holds nothing you need.
The same applies to init scripts. Scripts in /docker-entrypoint-initdb.d only run on an empty data directory. If one of them failed on the first start and the container was restarted, the rest never ran, and the database they were supposed to create is missing.
Restores, renames and dropped databases
- Restore into an existing database. Plain
pg_restore -d shopneedsshopto exist already. With pg_restore --create, you connect to another database (usuallypostgres), and pg_restore creates the target database under its original name. Its database-level settings and privileges come back too. - Renamed databases. After
ALTER DATABASE ... RENAME TO, every client still using the old name gets this error. Pools and long-lived processes may keep working on old connections and only fail when they reconnect, sometimes hours later. - Just dropped. If the database disappears while a connection is being set up, the server adds the detail It seems to have just been dropped or renamed (see postinit.c).
- A different error. cannot connect to invalid database is not this problem. It means a
DROP DATABASEwas interrupted, and since the August 2023 minor releases (such as 15.4) such a database is marked invalid instead of being left corrupt. The hint tells you to finish the job withDROP DATABASE.
If a connection pooler sits in front of PostgreSQL, the name the application uses may be an alias that the pooler maps to a real database. Check that mapping too; our PgBouncer guide shows where it lives.
On large production fleets
On a laptop, this error is a typo you fix in a second. With dozens of services, several clusters and automated deployments, it turns into an outage pattern of its own:
- Check every node, not just the primary. Physical replicas have the same databases as their primary, but a new cluster built from a logical dump, a logical replication target or a restored test copy may not. Run a check like the one below on every target before you switch traffic.
- Retry storms. A wrong database name fails only after the server has started a backend process and authenticated the client. Hundreds of pods retrying in a tight loop add CPU load and connection churn, and fill the log. In our experience, backoff in the client matters more here than any server setting.
- Watch for 3D000 in the logs. With
%einlog_line_prefix, you can count these errors by SQLSTATE and alert when they appear after a deploy or failover. - Keep names in one place. Store the database name in the same config source as the host and credentials, not as a default hidden in a driver or an image.
-- before a release: every database the apps expect, on every node
SELECT d AS expected_db,
EXISTS (SELECT 1 FROM pg_database WHERE datname = d) AS present
FROM unnest(ARRAY['shop', 'billing', 'reporting']) AS d;
Key takeaways
- 3D000 means there is no database with exactly that name in the cluster you reached.
- libpq and psql default the database name to the user name, so
psql -U applooks for a database calledapp. - Connect to
postgresand checkpg_databaseorpsql -lto see what really exists. - Quoted names keep their case, and Docker's
POSTGRES_DBonly works on an empty data directory. - After restores, renames and failovers, check that every expected database exists on every node.