Incident Response

PostgreSQL FATAL: database does not exist

The server is up, the password is right, and PostgreSQL still turns the connection away. Usually the client asked for a database nobody created, often one named after the user. Here is where that name comes from, how to check what exists, and the Docker, case and restore traps behind this error.

· 11 min read · AG Data Team

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:

  1. dbname in the connection string or URI, or -d on the command line;
  2. the PGDATABASE environment variable;
  3. 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

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:

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

PostgreSQL Errors Connections Docker
Work with us

Applications cannot reach their databases after a move?

We run PostgreSQL migrations, restores and failovers so every service finds its database, role and access rules on the first try.

Get in touch More from the blog