How to fix P1009: Database `{database_name}` already exists on the database server at `{database_host}:{database_port}` in Prisma

PrismaBEGINNERMEDIUMUpdated Dec 22, 2025ยท 131 views

This error occurs when Prisma attempts to create a database that already exists on your database server. Learn how to resolve conflicts and properly initialize your database schema.

Quick fix

Fixes P1009: Database `{database_name}` already exists on the database server at `{database_host}:{database_port}`

psql -U your_username -h localhost -l
  1. Verify the database actually exists
  2. Option 1: Use the existing database (recommended for development)
  3. Option 2: Drop and recreate the database
  4. Option 3: Change the database name
prisma โ€” error
psql -U your_username -h localhost -l
P1009: Database `{database_name}` already exists on the database server at `{database_host}:{database_port}`

What this error means

The P1009 error is thrown by Prisma when you try to create a database that has already been created on the specified database server. This typically happens during database initialization or migration operations when Prisma Migrate or related commands attempt to execute CREATE DATABASE statements. This error indicates that a database with the exact name specified in your connection string already exists at the given host and port location. Unlike schema-level conflicts, this is a database-level issue that occurs before Prisma can even connect to work with tables and migrations. The error is most commonly encountered when running commands like `prisma migrate dev`, `prisma db push`, or custom database setup scripts that include database creation logic. It's a protective measure to prevent accidental database overwrites.

Typical symptoms

  • Prisma migration commands fail with P1009 error code
  • Database initialization scripts throw "database already exists" errors
  • Unable to run `prisma migrate dev` or `prisma db push` on a fresh setup
  • CI/CD pipelines fail during database provisioning steps
  • Development environment setup fails when following onboarding instructions

Common causes

  1. Running database creation commands multiple times without cleanup
  2. Database persisted from previous development sessions or tests
  3. Connection string points to an existing production or staging database
  4. Team members sharing the same database server with conflicting database names
  5. Incomplete cleanup after running `prisma migrate reset` or similar commands
  6. Docker containers restarting with persisted volumes containing the database
  7. Automated scripts attempting to create databases that infrastructure already provisioned

How to fix "P1009: Database `{database_name}` already exists on the database server at `{database_host}:{database_port}`"

1Verify the database actually exists

First, confirm that the database mentioned in the error message exists on your database server.

For PostgreSQL:

bash
psql -U your_username -h localhost -l

For MySQL:

bash
mysql -u your_username -p -e "SHOW DATABASES;"

Check if the database name from your DATABASE_URL appears in the list. If it does exist, you have several options depending on your use case.

2Option 1: Use the existing database (recommended for development)

If you're in a development environment and want to work with the existing database, simply skip the database creation step. Prisma Migrate will work with the existing database.

Run migrations on the existing database:

bash
npx prisma migrate dev

If the schema is out of sync, you may need to baseline it:

bash
# Create a migration without applying it
npx prisma migrate dev --create-only

# Mark it as applied
npx prisma migrate resolve --applied "migration_name"

For databases that were manually created or modified, use baseline:

bash
npx prisma migrate dev --name baseline
3Option 2: Drop and recreate the database

If you're in a development environment and want a clean slate, drop the existing database and let Prisma recreate it.

WARNING: This will delete all data in the database.

For PostgreSQL:

bash
psql -U your_username -h localhost -c "DROP DATABASE your_database_name;"

For MySQL:

bash
mysql -u your_username -p -e "DROP DATABASE your_database_name;"

Then run Prisma Migrate:

bash
npx prisma migrate dev

Or use Prisma's built-in reset command (drops and recreates automatically):

bash
npx prisma migrate reset
4Option 3: Change the database name

If you need to work with a different database, update your connection string to use a new database name.

In your .env file, modify the DATABASE_URL:

PostgreSQL:

env
# Before
DATABASE_URL="postgresql://user:password@localhost:5432/myapp"

# After
DATABASE_URL="postgresql://user:password@localhost:5432/myapp_dev"

MySQL:

env
# Before
DATABASE_URL="mysql://user:password@localhost:3306/myapp"

# After
DATABASE_URL="mysql://user:password@localhost:3306/myapp_dev"

Then run:

bash
npx prisma migrate dev

This creates a new database with the updated name.

5Option 4: Fix automated scripts and CI/CD

If you're encountering this in automated environments, ensure your scripts check for database existence before attempting creation.

For PostgreSQL scripts:

sql
CREATE DATABASE IF NOT EXISTS your_database_name;

For MySQL scripts:

sql
CREATE DATABASE IF NOT EXISTS your_database_name;

In Node.js scripts using Prisma:

typescript
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function setupDatabase() {
  try {
    // Test connection - if it works, database exists
    await prisma.$connect();
    console.log('Database already exists, skipping creation');
  } catch (error) {
    // Database doesn't exist, create it
    console.log('Creating database...');
    // Use your database creation logic here
  } finally {
    await prisma.$disconnect();
  }
}

For Docker Compose environments, ensure volumes are cleaned:

bash
docker-compose down -v  # Removes volumes
docker-compose up -d

Advanced notes

Environment-specific considerations:

- Production databases: Never drop production databases. Always use Option 1 (baselining) to sync your Prisma schema with existing production data.

- Team environments: When multiple developers share a database server, use environment-specific database names (e.g., myapp_john, myapp_jane) to avoid conflicts.

- Shadow database: Prisma Migrate uses a shadow database for migrations. If you encounter P1009 with the shadow database, ensure your database user has permissions to create databases, or configure shadowDatabaseUrl in your Prisma schema to point to a dedicated shadow database.

- Serverless environments: Some managed database providers (like PlanetScale, Neon, or Supabase) automatically create databases. In these cases, you typically never need to create the database yourself - just run migrations.

- Multi-schema setups: If you're using PostgreSQL schemas (not to be confused with Prisma schema), ensure your connection string points to the correct schema. The P1009 error refers to database-level conflicts, not schema-level.

Database provider differences:

- PostgreSQL: Strictly enforces database uniqueness at the cluster level
- MySQL: Database names are case-sensitive on Linux/Unix but case-insensitive on Windows
- SQLite: This error doesn't apply - SQLite uses file-based databases

Migration history sync:

If you've previously used prisma db push (which bypasses migrations), switching to prisma migrate dev may cause conflicts. Run prisma migrate resolve to mark existing schema changes as applied without re-executing them.

Frequently asked questions

What does "P1009: Database `{database_name}` already exists on the database serve" mean?
The P1009 error is thrown by Prisma when you try to create a database that has already been created on the specified database server. This typically happens during database initialization or migration operations when Prisma Migrate or related commands attempt to execute CREATE DATABASE statements. This error indicates that a database with the exact name specified in your connection string already exists at the given host and port location.
How do I fix "P1009: Database `{database_name}` already exists on the database serve"?
This error occurs when Prisma attempts to create a database that already exists on your database server. Learn how to resolve conflicts and properly initialize your database schema.
What causes "P1009: Database `{database_name}` already exists on the database serve"?
Running database creation commands multiple times without cleanup Database persisted from previous development sessions or tests Connection string points to an existing production or staging database
Is the "P1009: Database `{database_name}` already exists on the database serve" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading