Skip to content

How to Fix PostgreSQL Database Already Exists Error

DodaTech Updated 2026-06-24 3 min read

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

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. 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

### Why does PostgreSQL not have CREATE DATABASE IF NOT EXISTS?

PostgreSQL's DDL statements are intentionally simple. Database creation involves filesystem operations that are not easily wrapped in a conditional. The community recommends using shell scripts or PL/pgSQL wrappers.

Can I create a database with the same name as an existing one?

No. Each database name must be unique within a PostgreSQL cluster. Drop the existing database first or use a different name.

What happens to connections when I drop a database?

All connections to the dropped database are terminated immediately. Ensure no active connections exist before dropping. Use SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'mydb'; to disconnect all users first.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro