This error occurs when attempting to create a database with a name that already exists in your PostgreSQL cluster. Use IF NOT EXISTS or choose a different database name to resolve it.
PostgreSQL raises a "duplicate_database" error (code 42P04) when you try to create a new database using CREATE DATABASE with a name that is already in use in the same cluster. This safety mechanism prevents accidental data loss and confusion from multiple databases sharing the same name. The error is intentional and protects against overwriting or conflicting with existing databases.
Connect to PostgreSQL and view all databases:
\lOr use SQL directly:
SELECT datname FROM pg_database WHERE datistemplate = false;This will show you all non-template databases in your cluster and help you identify if the database you're trying to create already exists.
Modify your CREATE DATABASE statement to use the IF NOT EXISTS clause:
CREATE DATABASE my_database IF NOT EXISTS;This tells PostgreSQL to create the database only if it doesn't already exist, preventing the error without requiring manual intervention.
If you need a new separate database and the name conflicts, select a different name:
CREATE DATABASE my_database_v2;Or include a timestamp or environment identifier in the name:
CREATE DATABASE my_database_prod_2025;If the existing database is no longer needed, you can drop it before creating a new one:
DROP DATABASE my_database;
CREATE DATABASE my_database;WARNING: This permanently deletes all data in that database. Make sure you have backups if the data is important.
If you get an error that the database is in use, you must close all active connections first:
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'my_database' AND pid != pg_backend_pid();
DROP DATABASE my_database;This terminates all sessions connected to the database, allowing the DROP to proceed.
When copying databases, use the CREATE DATABASE with TEMPLATE syntax instead of recreating from scratch:
CREATE DATABASE my_database_copy WITH TEMPLATE original_database;This is faster and more reliable than rebuilding. If the database copy destination already exists, use IF NOT EXISTS first. In automated deployment scripts (Docker, CI/CD), always include IF NOT EXISTS to make scripts idempotent—they can run multiple times safely. Some tools like Django and Doctrine ORM have specific handling for this error code (42P04) and will retry or skip creation appropriately when properly configured.
ERROR: syntax error at end of input
Syntax error at end of input in PostgreSQL
Bind message supplies N parameters but prepared statement requires M
Bind message supplies N parameters but prepared statement requires M in PostgreSQL
Multidimensional arrays must have sub-arrays with matching dimensions
Multidimensional arrays must have sub-arrays with matching dimensions
ERROR: value too long for type character varying
Value too long for type character varying
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL