PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 42P04: database already exists

CREATE DATABASE was run for a database name that already exists.

PostgreSQL 17.12
42P04: database "shop" already exists

Jump to a cause

  1. 01CREATE DATABASE for a name that already exists (keep the existing one)
  2. 02Rerunning an init script for a test database

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

PostgreSQL stops with ERROR: 42P04: database "shop" already exists when you create a database whose name is already taken. PostgreSQL has no CREATE DATABASE IF NOT EXISTS, so setup scripts that run more than once hit this often.

First check pg_database to see whether the database is really there. Then decide: keep it and skip the CREATE, or drop it and start fresh. Both ways below were run on PostgreSQL 17.

01CREATE DATABASE for a name that already exists (keep the existing one)

shop already exists, and CREATE DATABASE shop fails with 42P04. SELECT datname FROM pg_database WHERE datname = 'shop' returned one row.

To create it only when missing, build the statement with SELECT and run it with psql's \gexec: SELECT 'CREATE DATABASE shop' WHERE NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = 'shop')\gexec. Because shop existed, the SELECT returned no row, nothing was executed, and there was no error.

Setup
Setuprun as postgres
DROP DATABASE IF EXISTS shop;
CREATE DATABASE shop;
OUTPUT
NOTICE:  00000: database "shop" does not exist, skipping
DROP DATABASE
CREATE DATABASE
Reproducerun as postgres
CREATE DATABASE shop;
✕ ERROR
ERROR:  42P04: database "shop" already exists
Checkrun as postgres
SELECT datname FROM pg_database WHERE datname = 'shop';
OUTPUT
 datname
---------
 shop
(1 row)

\gexec is a psql command. In other clients, check pg_database first in your code and send CREATE DATABASE only when the row is missing.

After the fixrun as postgres
SELECT 'CREATE DATABASE shop' WHERE NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = 'shop')\gexec
SELECT datname FROM pg_database WHERE datname = 'shop';
DROP DATABASE shop;
✓ SUCCESS
 datname
---------
 shop
(1 row)

DROP DATABASE

02Rerunning an init script for a test database

A script that creates shop_test fails with 42P04 on the second run. If this database can be thrown away, drop it first.

pg_stat_database showed numbackends 0 for shop_test, meaning nobody was connected. Then DROP DATABASE IF EXISTS shop_test; CREATE DATABASE shop_test; ran without error.

Setup
Setuprun as postgres
DROP DATABASE IF EXISTS shop_test;
CREATE DATABASE shop_test;
OUTPUT
NOTICE:  00000: database "shop_test" does not exist, skipping
DROP DATABASE
CREATE DATABASE
Reproducerun as postgres
CREATE DATABASE shop_test;
✕ ERROR
ERROR:  42P04: database "shop_test" already exists
Checkrun as postgres
SELECT datname, numbackends FROM pg_stat_database WHERE datname = 'shop_test';
OUTPUT
  datname  | numbackends
-----------+-------------
 shop_test |           0
(1 row)

DROP DATABASE deletes all data in it. Use this only for databases you can recreate, such as test databases.

After the fixrun as postgres
DROP DATABASE IF EXISTS shop_test;
CREATE DATABASE shop_test;
DROP DATABASE shop_test;
✓ SUCCESS
DROP DATABASE
CREATE DATABASE
DROP DATABASE

FAQ

Does PostgreSQL have CREATE DATABASE IF NOT EXISTS?

No. In psql, use SELECT 'CREATE DATABASE shop' WHERE NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = 'shop')\gexec. It runs the CREATE only when the database is missing.

How do I check if a database exists in PostgreSQL?

SELECT datname FROM pg_database WHERE datname = 'shop'; returns one row if it exists and zero rows if not.

How do I recreate a database in a setup script?

Use DROP DATABASE IF EXISTS name; before CREATE DATABASE name;. This deletes the old data, so use it only for test databases. Check numbackends in pg_stat_database to see whether anyone is still connected.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12