How to Create a Database in SQL

What you’ll build or solve

You’ll create a new database using the CREATE DATABASE statement.

When this approach works best

This approach works best when:

  • You are starting a new application or service.
  • You need separate development, staging, or production environments.
  • You want isolated storage for testing or reporting.

This is not necessary if you only need a new table inside an existing database.

Prerequisites

  • Access to a SQL database server such as MySQL, PostgreSQL, SQL Server, or SQLite
  • Permission to create databases
  • A database client or terminal
  • Connected to the database server

Example connection (MySQL):

mysql-u username-p-h localhost

Example connection (PostgreSQL):

psql-U username-h localhost

Step-by-step instructions

Step 1: Execute the CREATE DATABASE statement

Run the CREATE DATABASE command while connected to the server.

MySQL and PostgreSQL

CREATE DATABASE my_database;

If you want to avoid errors when the database already exists:

CREATE DATABASEIFNOTEXISTS my_database;

Note: Some PostgreSQL versions do not support IF NOT EXISTS.


SQL Server

CREATE DATABASE my_database;
GO

GO executes the batch in SQL Server tools.


SQLite

SQLite creates a database when you create or open a file:

sqlite3 my_database.db

The file becomes the database.


What to look for

After running CREATE DATABASE:

  • If successful, no error appears.
  • If you see “permission denied” or “access denied,” your user lacks CREATE privileges.
  • If the database already exists, you may get an error unless using IF NOT EXISTS.

After creation, you typically switch to the new database before creating tables.

MySQL:

USE my_database;

PostgreSQL:

\c my_database

To verify it exists:

MySQL:

SHOW DATABASES;

PostgreSQL:

Switching and verifying are common workflow steps but not required for creation itself.


Examples you can copy

Example 1: Create a development database

CREATE DATABASE app_dev;

Example 2: Create a database safely in setup scripts

CREATE DATABASEIFNOTEXISTS analytics_db;

Useful when running automated deployments.


Example 3: Create a database with character set (MySQL)

CREATE DATABASE my_database
CHARACTERSET utf8mb4
COLLATE utf8mb4_unicode_ci;

This configures full Unicode support.


Example 4: Create a reporting database in PostgreSQL

CREATE DATABASE reporting_db;

You can then connect using:

\c reporting_db

Common mistakes and how to fix them

Mistake 1: Missing privileges

You might run:

CREATE DATABASE test_db;

And receive a permission error.

Why it breaks:

Your user does not have CREATE rights.

Correct approach:

Ask an administrator to grant permission.

Example in MySQL:

GRANTCREATEON*.*TO'username'@'localhost';

Mistake 2: Confusing database with table

You might write:

CREATE DATABASE users;

Why it breaks:

You intended to create a table.

Correct approach:

CREATETABLE users (
  idINTPRIMARYKEY,
  nameVARCHAR(100)
);

Use CREATE DATABASE for databases and CREATE TABLE for tables.


Mistake 3: Running the command in the wrong context

You might attempt:

CREATE DATABASE my_database;

Inside a restricted environment such as shared hosting.

Why it breaks:

Some managed services do not allow database creation from SQL.

Correct approach:

Create the database through the hosting control panel or cloud provider dashboard.


Troubleshooting

If you see “access denied,” verify that your user has CREATE privileges.

If the database does not appear in listings, confirm you are connected to the correct server instance.

If IF NOT EXISTS fails in PostgreSQL, remove it and check existence manually.

If SQLite does not create a file, verify write permissions in the directory.

If SQL Server shows an error near GO, confirm you are running the command in a compatible client.


Quick recap

  • Connect to your database server.
  • Run CREATE DATABASE database_name;.
  • Use IF NOT EXISTS when supported.
  • Switch to the database before creating tables.
  • Confirm you have proper permissions if errors occur.