How to Fix PostgreSQL Database Already Exists Error
In this tutorial, you'll learn about How to Fix PostgreSQL Database Already Exists Error. We cover key concepts, practical examples, and best practices.
The Problem
You run a setup script or migration and PostgreSQL returns:
ERROR: database "mydb" already exists
Or from createdb:
createdb: error: database creation failed: ERROR: database "mydb" already exists
PostgreSQL does not support CREATE DATABASE IF NOT EXISTS, so attempting to create a database that already exists always fails.
Quick Fix
1. Drop the database first (if safe)
If you want to recreate the database from scratch:
-- Wrong — fails if database exists
CREATE DATABASE mydb;
-- Right — drop then create
DROP DATABASE IF EXISTS mydb;
CREATE DATABASE mydb;
Warning: This deletes all data in the database.
2. Use a shell script to check existence
Use a shell function to check before creating:
#!/bin/bash
# psql wrapper that creates DB only if it does not exist
if psql -U postgres -lqt | cut -d \| -f 1 | grep -qw mydb; then
echo "Database mydb already exists"
else
createdb -U postgres mydb
echo "Database mydb created"
fi
3. Use template databases
Create the database with IF NOT EXISTS behavior using PL/pgSQL:
DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb') THEN
PERFORM dblink_exec('dbname=postgres', 'CREATE DATABASE mydb');
END IF;
END $$;
This requires the dblink extension:
CREATE EXTENSION IF NOT EXISTS dblink;
4. Handle it in migration tools
If using a migration framework, configure it to handle existing databases:
# Rails — skip creation if exists
rails db:create --skip-if-exists
# Django — use django-extensions
python manage.py create_database mydb
Or in Flyway:
flyway -createDatabase=false migrate
5. Connect to the existing database
If the database exists, connect to it instead of trying to create it:
# Wrong — tries to create
createdb mydb
# Right — simply connect
psql -d mydb
Prevention
- Use idempotent setup scripts that check for database existence before creating.
- Use migration tools that handle database creation idempotently.
- Document the database creation process in your README.
- Use environment variables for database names to avoid hardcoding.
Common Mistakes with database exists
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world POSTGRES code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro