How to fix ERROR 1007: Can't create database; database exists in MySQL

MySQLBEGINNERLOWUpdated Dec 22, 2025ยท 430 views

This error occurs when you attempt to create a database with CREATE DATABASE but a database with that name already exists. The fix is to use CREATE DATABASE IF NOT EXISTS to make the statement idempotent, or drop the existing database first if you want to start fresh.

Quick fix

Fixes ERROR 1007: Can't create database; database exists

SHOW DATABASES;
  1. Check existing databases
  2. Use IF NOT EXISTS in your CREATE statement (Recommended)
  3. Drop and recreate if starting fresh
  4. For backup restoration scripts
mysql โ€” error
SHOW DATABASES;
ERROR 1007: Can't create database; database exists

What this error means

MySQL error 1007 (SQLSTATE: HY000, ER_DB_CREATE_EXISTS) is a protective mechanism that prevents duplicate databases from being created on your server. When you execute a CREATE DATABASE statement, MySQL checks if a database with that name already exists. If it does, the operation fails immediately with this error to preserve data integrity and prevent accidental overwrites.

Typical symptoms

  • Database creation scripts fail on second run
  • Deployment or backup restoration fails when creating databases
  • Automated setup scripts error out but don't indicate why
  • CI/CD pipelines stop at database initialization step

Common causes

  1. The CREATE DATABASE statement lacks an IF NOT EXISTS clause and the database was previously created
  2. A backup restoration script includes CREATE DATABASE for a database that already exists
  3. Environment variable confusion (e.g., $DBname vs $DBName) causes script to attempt recreation
  4. On case-sensitive systems, database name case mismatch (e.g., "Sales" vs "sales") creates apparent duplicates
  5. Multiple developers or processes execute database creation statements on the same server

How to fix "ERROR 1007: Can't create database; database exists"

1Check existing databases

First, verify what databases currently exist on your MySQL server:

sql
SHOW DATABASES;

Look through the list to confirm whether the database name you're trying to create already exists. On Unix systems, names are case-sensitive, so "myapp" and "MyApp" are different databases.

2Use IF NOT EXISTS in your CREATE statement (Recommended)

Modify your CREATE DATABASE statement to include the IF NOT EXISTS clause:

sql
CREATE DATABASE IF NOT EXISTS myapp;

This syntax tells MySQL to create the database only if it doesn't already exist. If the database exists, the statement succeeds silently without error. This is the safest approach for scripts that may run multiple times.

3Drop and recreate if starting fresh

If you want to completely reset the database (delete all data), use DROP DATABASE IF EXISTS before creating:

sql
DROP DATABASE IF EXISTS myapp;
CREATE DATABASE myapp;

The IF EXISTS clause prevents errors if the database doesn't exist. Use this only when you're certain all data in the database should be deleted.

4For backup restoration scripts

When restoring from a mysqldump backup, you have two options:

Option 1: Modify the dump file before importing. Open the SQL dump file in a text editor and remove or modify the CREATE DATABASE line to include IF NOT EXISTS:

sql
CREATE DATABASE IF NOT EXISTS myapp;

Option 2: Add the --add-drop-database flag when creating the dump initially (for future backups):

bash
mysqldump --add-drop-database -u root -p myapp > backup.sql

This automatically includes DROP DATABASE IF EXISTS in the dump file.

5Use the existing database

If you don't need to recreate the database and just need to work with it, simply select it:

sql
USE myapp;

You can verify which database is currently selected with:

sql
SELECT DATABASE();

Advanced notes

On Unix/Linux systems, database names are case-sensitive by default, while Windows treats them case-insensitively. This means on Unix, "Sales" and "sales" are two distinct databases, but they would collide on Windows. Always use consistent casing in your scripts and connection strings. For high-availability setups with replication, ensure all replica servers have the IF NOT EXISTS clause in their initialization scripts to prevent cascade failures across the replication chain. When using Docker or containerized MySQL, the initialization scripts (typically in /docker-entrypoint-initdb.d/) should always use IF NOT EXISTS to allow containers to be recreated without manual cleanup.

Frequently asked questions

What does "ERROR 1007: Can't create database; database exists" mean?
MySQL error 1007 (SQLSTATE: HY000, ER_DB_CREATE_EXISTS) is a protective mechanism that prevents duplicate databases from being created on your server. When you execute a CREATE DATABASE statement, MySQL checks if a database with that name already exists. If it does, the operation fails immediately with this error to preserve data integrity and prevent accidental overwrites.
How do I fix "ERROR 1007: Can't create database; database exists"?
This error occurs when you attempt to create a database with CREATE DATABASE but a database with that name already exists. The fix is to use CREATE DATABASE IF NOT EXISTS to make the statement idempotent, or drop the existing database first if you want to start fresh.
What causes "ERROR 1007: Can't create database; database exists"?
The CREATE DATABASE statement lacks an IF NOT EXISTS clause and the database was previously created A backup restoration script includes CREATE DATABASE for a database that already exists Environment variable confusion (e.g., $DBname vs $DBName) causes script to attempt recreation
Is the "ERROR 1007: Can't create database; database exists" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading