Skip to content

FATAL: database does not exist (3D000) in PostgreSQL, Fixed

FATAL: database does not exist (3D000) usually means you never named a database and the client picked one for you, or you named it in the wrong case, or on the wrong server. How to tell which in one query, and fix it.

database-does-not-exist sqlstate-3d000 createdb psql pg-connections postgresql postgresql-errors
Gopi Gorantala
Reading Progress

On This Page

I was sure someone had dropped it. The invoice archive's dev database was there yesterday, the nightly import had written to it, and now every connection came back with FATAL: database "invoicearchive" does not exist. I'd already opened the audit log to find the culprit before I ran \l and saw it sitting right there, in the list, spelled InvoiceArchive.

Nobody dropped anything. The name had capital letters, and the connection string didn't.

That's the rarest way to meet this error. The most common is that you never asked for a database at all, and the client picked one for you.

FATAL: database does not exist, as psql prints it

From psql 16.15 (libpq 16.15, Ubuntu 24.04) against a PostgreSQL 16.15 server with lc_messages = 'C':

$ psql -h 127.0.0.1 -p 5440 -U invoice_app -c 'select 1'
psql: error: connection to server at "127.0.0.1", port 5440 failed: FATAL:  database "invoice_app" does not exist

Look at that command again. There's no database in it. The server is complaining about a database called invoice_app, which happens to be the user name. Hold that thought.

The same error from inside a session, when \c can't switch:

\connect: connection to server at "127.0.0.1", port 5440 failed: FATAL:  database "invoicearchive" does not exist

And the server log, with %e in log_line_prefix so the SQLSTATE shows up:

2026-12-02 22:08:41.746 CET [422] invoice_app@OrdersDb 3D000 FATAL:  database "OrdersDb" does not exist

The SQLSTATE is 3D000 (invalid_catalog_name in the error codes appendix). FATAL means the backend gave up during connection setup and the session never started. Old clients print less: psql 11 showed psql: FATAL: database "x" does not exist. Search on database "x" does not exist, the part that never changes. A server with translated messages prints it in its own language; the SQLSTATE stays 3D000.

DROP DATABASE on a missing name gives the same SQLSTATE as an ERROR inside a working session; same fix family. (How to read a PostgreSQL error covers the other fields.)

Is it the name, the server, or the default?

You got far. The network's fine, pg_hba.conf let you in, and authentication passed. PostgreSQL then looked up the database by name and found nothing. (If you're not sure this is the error you have and not one of its neighbours, this comparison of connection errors lines them up.)

So there are only three questions, and one connection to a database that always exists answers all of them:

psql -h 127.0.0.1 -p 5440 -U invoice_app -d postgres -Atc \
  "select inet_server_port(), current_setting('cluster_name')"
psql -h 127.0.0.1 -p 5440 -U invoice_app -d postgres -Atc \
  "select datname from pg_database where datname ilike '%invoice%'"

postgres is created by initdb on every cluster, so it's a safe place to land. If it's been dropped, use template1. Any role that can log in can read pg_database; no superuser needed. And psql -l does the listing for you, because it connects to postgres by itself.

  • The name isn't in the list at all → you're on the wrong server, or it was never created.
  • It's there in different case → quoting.
  • Your command never named a database → the default kicked in.

One trap while you're here: pg_isready says accepting connections even when the database you pass doesn't exist. It only checks that the server answers. Don't use it as proof that invoice_archive is there.

Six ways to ask for a database that isn't there

Everything below ran on a throwaway cluster (pg_createcluster 16 dblab -p 5440), with one role, invoice_app, and two databases: "InvoiceArchive" (created quoted) and invoice_archive_dev. Don't do this against a shared server.

CREATE ROLE invoice_app LOGIN PASSWORD 'lab-only-pw';
CREATE DATABASE "InvoiceArchive" OWNER invoice_app;  -- quoted: capitals kept
CREATE DATABASE invoice_archive_dev OWNER invoice_app;
CREATE DATABASE OrdersDb OWNER invoice_app;          -- unquoted: stored as ordersdb

1. No database named. psql -U invoice_app with no -d. libpq's dbname defaults to the user name, so it asked for invoice_app. That's the first block in this post.

2. Quoted at creation, lowercase at connection.

$ psql -h 127.0.0.1 -p 5440 -U invoice_app -d invoicearchive -c 'select 1'
psql: error: connection to server at "127.0.0.1", port 5440 failed: FATAL:  database "invoicearchive" does not exist

3. Unquoted at creation, capitals at connection. This is the one that surprised me.

$ psql -h 127.0.0.1 -p 5440 -U invoice_app -d OrdersDb -c 'select 1'
psql: error: connection to server at "127.0.0.1", port 5440 failed: FATAL:  database "OrdersDb" does not exist

And yet -d InvoiceArchive, capitals and all, connected fine.

4. An environment variable you forgot. PGDATABASE=invoice_archive in a shell profile overrides the default, and psql asks for that instead. It printed database "invoice_archive" does not exist, exactly like a typo would.

5. The wrong server. I started a second cluster on 5432 with the same role but none of the databases, then left the port off:

$ psql -h 127.0.0.1 -U invoice_app -d invoice_archive_dev -c 'select 1'
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL:  database "invoice_archive_dev" does not exist

Correct name. Wrong cluster. A Homebrew Postgres on 5432 while your container's published on 5433 produces exactly this.

6. createdb creating the wrong one. I ran createdb -h 127.0.0.1 -p 5440 -U invoice_app as Linux root, expecting it to create invoice_app (the database psql was asking for). With -e to echo the SQL:

CREATE DATABASE root;

It made root. Then psql still failed with database "invoice_app" does not exist. More on why in a moment.

Teardown: pg_dropcluster 16 dblab --stop deletes the lab cluster and its data, so only run it on the throwaway one. Stop and clean up any second cluster you started for step 5 the same way.

Plainly put

PostgreSQL got a database name in the startup packet, compared it byte for byte with the names in pg_database, and found no match. Either nobody sent the name you meant, or the name you sent isn't spelled that way on that server.

Who picks the name, and when case matters

Two separate mechanisms make this confusing, and forum answers usually explain neither.

The first is defaults. When you leave out the database, libpq fills it in: dbname defaults to the user name, and the user name defaults to your OS login. So psql typed as alice asks for role alice and database alice. That's why the error so often names you, or root, or postgres. pgJDBC does the same thing: the driver docs say the default database has "the same name as the user name used to connect". A Spring spring.datasource.url of jdbc:postgresql://localhost:5432/ with username=invoice_app ends up asking for invoice_app, and you get PSQLException: FATAL: database "invoice_app" does not exist passed through from the server. I didn't run the Java side this time (no driver jar in my sandbox); the message is the server's, so the text is the same.

createdb breaks the pattern, and that's what step 6 showed. The createdb docs say the default name is "the same name as the current system user", and PGUSER or PGDATABASE override it. The -U flag doesn't. So createdb -U invoice_app run as root creates root, while psql -U invoice_app asks for invoice_app. Two tools, two defaults, one shell prompt. I lost twenty minutes to that before I added -e and read the SQL it sent.

The second mechanism is case. In SQL, an unquoted name is folded to lowercase: CREATE DATABASE OrdersDb stores ordersdb. Double quotes keep it exactly: "InvoiceArchive" stays InvoiceArchive. But the database name in a connection (-d, dbname=, the path of a postgresql:// URI, a JDBC URL) is never parsed as SQL. It's a plain string, compared exactly. So:

Created with Stored as Connect with Result
CREATE DATABASE OrdersDb ordersdb -d ordersdb works
CREATE DATABASE OrdersDb ordersdb -d OrdersDb 3D000
CREATE DATABASE "InvoiceArchive" InvoiceArchive -d InvoiceArchive works
CREATE DATABASE "InvoiceArchive" InvoiceArchive -d invoicearchive 3D000

SQL folds, connections don't. Once you see that, the whole case story is one rule. Tables follow the folding half of it too, which is why relation does not exist has the same flavour.

Docker adds a quieter way. The official image creates a database only when the data directory is empty, and POSTGRES_DB falls back to POSTGRES_USER when unset. Add POSTGRES_DB=invoice_archive to a Compose file whose volume was initialised last month and nothing happens: init is skipped, same as a changed POSTGRES_PASSWORD being ignored. My sandbox had no Docker daemon, so no container output here.

Name the database, then make sure it's on that server

Fix the cause you found in section two.

You relied on a default. Name the database every time: -d, dbname=, the path in a URI or JDBC URL. If you want a database named after your user, tell createdb the name outright (createdb -U invoice_app invoice_app) rather than trusting its default. And check echo $PGDATABASE $PGUSER.

Case mismatch. Pick lowercase and rename once, while nothing is connected to it:

-- connect to postgres, not to the database being renamed
ALTER DATABASE "InvoiceArchive" RENAME TO invoice_archive;

ALTER DATABASE … RENAME needs ownership plus CREATEDB (or superuser), fails if anyone is connected to that database, and breaks every connection string that still uses the old name. Grep your configs before you run it. The cheaper fix is to change the connection string to match the stored name exactly. Fine for a laptop, but it leaves the trap there for the next person.

Wrong server. Fix the host and port, not the database. inet_server_port() and cluster_name (or version()) tell you where you landed. On a Mac with both Homebrew and Docker Postgres, decide which one owns 5432 and move the other.

It was never created. Create it, as the role that should own it:

CREATE DATABASE invoice_archive OWNER invoice_app;

Your role needs CREATEDB or superuser for that. On RDS, Cloud SQL or Azure there's no superuser, so use the admin role the service gave you.

Docker volume already initialised. Create the database with docker exec -it <container> psql -U <user> -d postgres -c 'CREATE DATABASE …', or put the CREATE DATABASE in a migration. Deleting the volume so the entrypoint runs again also works, and also deletes every row in it. Only do that when the data really is disposable.

What I wouldn't do: connect the app to postgres "for now". It's the maintenance database that createdb, psql -l and plenty of tools land on. Your tables in there will outlive "for now".

One name, lowercase, written down once

Database names lowercase with underscores, created unquoted by a provisioning script, never by hand in a GUI that quotes for you. Every connection string names the database. One config source per environment, and no PGDATABASE lurking in someone's .zshrc.

# application-dev.properties
spring.datasource.url=jdbc:postgresql://localhost:5433/invoice_archive
spring.datasource.username=invoice_app

For Compose, I put the database name in POSTGRES_DB for the first boot and in a migration for every boot after, so a reused volume can't skip it. If your connection strings come in more than one format, the formats compared shows where the database goes in each.

A check that runs before the app does

Add a startup probe that connects with the app's real connection string and runs select current_database(). It costs one round trip and fails with a readable 3D000 in the deploy log instead of a stack trace three services later. In CI, run migrations against a fresh container at the production version, so a missing CREATE DATABASE shows up there first.

Version-wise this error is very stable. The database "x" does not exist text and 3D000 are the same on every supported version (15 to 19 at the time of writing); what changed is libpq's wrapper. 14 and later print connection to server at "host", port N failed: in front, and 11 and earlier printed psql: FATAL: and nothing else. If you grep logs or match strings in tests, match on the SQLSTATE, not the wording.

What I checked: every variant above on PostgreSQL 16.15 with psql and createdb 16.15, the libpq, pgJDBC, createdb and Docker image docs for the defaults, and the old psql wording from a pgsql-general post. Not run: Java, Docker and Windows, which I described from the docs instead.

Pin these to the team wiki

  • Read the name in quotes first. If it's your user name, you never asked for a database.
  • psql -l, or a connection to postgres, shows what exists on the server you actually reached. Check the port while you're there.
  • SQL folds names to lowercase; connection strings don't. Keep database names lowercase and the problem's gone.
  • createdb defaults to your OS user, psql defaults to -U. They're not the same.
  • POSTGRES_DB only works on an empty volume.

Questions that come up next

Why does psql say database "my username" does not exist when I never asked for it?

Because you didn't pass -d, so libpq used your user name as the database name, and your user name defaults to your OS login. Pass -d postgres to get in, then create the database you actually want. If the role itself is missing you'll get role does not exist first; that one has its own fixes.

How do I list all databases in PostgreSQL if I can't connect to mine?

Run psql -l (it connects to the postgres database), or connect with -d postgres and run \l or select datname from pg_database. Any login role can read that list. If postgres was dropped, use -d template1.

Are PostgreSQL database names case-sensitive?

Stored names are. CREATE DATABASE MyDb without quotes stores mydb; with quotes it stores MyDb. A connection string uses the name exactly as written, with no folding, so it has to match the stored spelling.

Why didn't Docker create my POSTGRES_DB database?

The official image only applies POSTGRES_DB when the data directory is empty. If the volume was initialised earlier, the variable is ignored. Create the database with CREATE DATABASE through docker exec, or remove the volume only if its data is disposable.

database-does-not-existsqlstate-3d000createdbpsqlpg-connectionspostgresqlpostgresql-errors

Staff engineer architecting Kafka and Flink platforms for banking, where a dropped event is a reconciliation break, not a retry. Through javahandbook.com, I share production-tested guidance on building reliable event-driven systems.

Comments