SQL Keywords

SQL CREATE DB

What is SQL CREATE DATABASE used for?

Creates a new, empty database in the current server instance.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL CREATE DB: Supported: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, Amazon Redshift, Snowflake. Partial/Not supported: SQLite (uses ATTACH DATABASE).

SQL CREATE DB Full Explanation

CREATE DATABASE (often shortened informally to CREATE DB) is a Data Definition Language (DDL) statement that establishes a brand-new logical database on the target server. Executing it allocates catalog entries, default system schemas, and the physical storage required for future tables, indexes, and other objects. The exact side effects, default settings, and optional clauses vary by SQL dialect, but the core behavior is the same: after a successful run, the database name becomes available for connections and object creation. Because it changes global server state, CREATE DATABASE usually demands elevated privileges (e.g., CREATEDB, sysadmin, or root) and cannot run inside an open transaction block in some engines. Attempting to create a database that already exists without the proper conditional clause raises an error.

SQL CREATE DB Syntax

CREATE DATABASE database_name;
-- Dialect-specific options
-- PostgreSQL
CREATE DATABASE database_name
  WITH OWNER = user_name
       TEMPLATE = template_db
       ENCODING = 'UTF8'
       LC_COLLATE = 'en_US.UTF-8'
       LC_CTYPE = 'en_US.UTF-8';

-- MySQL
CREATE DATABASE [IF NOT EXISTS] database_name
  [DEFAULT CHARACTER SET utf8mb4]
  [DEFAULT COLLATE utf8mb4_unicode_ci];

-- SQL Server
CREATE DATABASE database_name
  [ON PRIMARY ( NAME = logical_file_name, FILENAME = 'path\file.mdf' )]
  [COLLATE collation_name];

SQL CREATE DB Parameters

  • database_name (identifier) - Name of the database to create.
  • IF NOT EXISTS (keyword, MySQL/SQLite) - Skip creation if the database already exists.
  • OWNER (identifier, PostgreSQL) - Role that will own the database.
  • TEMPLATE (identifier, PostgreSQL) - Template database to clone.
  • ENCODING / CHARACTER SET (string) - Default character set.
  • COLLATE / LC_COLLATE / LC_CTYPE (string) - Default collation rules.
  • ON PRIMARY, FILENAME (SQL Server) - Filegroup and physical file options.

Example Queries Using SQL CREATE DB

-- Basic creation
CREATE DATABASE sales_analytics;

-- Create only if absent (MySQL)
CREATE DATABASE IF NOT EXISTS hr_platform;

-- PostgreSQL with options
CREATE DATABASE reporting
  WITH OWNER = analyst_role
       TEMPLATE = template0
       ENCODING = 'UTF8';

-- SQL Server with file specification
CREATE DATABASE inventory
 ON PRIMARY ( NAME = inventory_data,
              FILENAME = 'C:\Data\inventory_data.mdf',
              SIZE = 10MB );

Expected Output Using SQL CREATE DB

  • The server registers a new database entry
  • After execution you can connect to the database and start creating schemas, tables, and other objects
  • If a database with the same name already exists and no IF NOT EXISTS or equivalent clause is supplied, the engine returns an error

Use Cases with SQL CREATE DB

  • Spinning up isolated environments for testing or staging.
  • Segregating multi-tenant customer data by database.
  • Creating a fresh reporting warehouse before running ETL pipelines.
  • Automating setup scripts for CI/CD pipelines.

Common Mistakes with SQL CREATE DB

  • Forgetting elevated privileges, leading to permission errors.
  • Attempting to run inside a transaction block on engines that forbid it (e.g., PostgreSQL without autocommit).
  • Omitting IF NOT EXISTS when rerunning deployment scripts, causing failures.
  • Misconfiguring character set or collation, later requiring costly migrations.
  • Assuming SQLite supports CREATE DATABASE (it uses file attachment instead).

Related Topics

CREATE SCHEMA, DROP DATABASE, ALTER DATABASE, CREATE TABLE, GRANT

First Introduced In

SQL-92 standard; earlier vendor support existed in Sybase/SQL Server.

Frequently Asked Questions

What permissions are required to run CREATE DATABASE?

Most systems demand elevated rights. In PostgreSQL you need the CREATEDB attribute, in MySQL the CREATE privilege at the global level, and in SQL Server membership in the sysadmin or dbcreator role.

Can I specify character set and collation during database creation?

Yes. MySQL uses DEFAULT CHARACTER SET and DEFAULT COLLATE. PostgreSQL offers ENCODING, LC_COLLATE, and LC_CTYPE. Choosing correctly at creation avoids data-integrity issues later.

How do I avoid errors if the database already exists?

In MySQL include IF NOT EXISTS. In other engines write a conditional wrapper or drop the database beforehand. Failing to do so triggers a duplicate-object error.

Why does SQLite ignore CREATE DATABASE?

SQLite treats each file as a self-contained database. Opening or creating a new file automatically provides a database, so the statement is unnecessary and unsupported.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!