Skip to content
C

CREATE DATABASE


CREATE DATABASE

What CREATE DATABASE Does

CREATE DATABASE is the DDL statement that provisions a brand-new, top-level container in a database server. A database is the outermost unit of isolation: it owns its own set of schemas (or, in MySQL, is itself the namespace), its own storage files/tablespace, its own set of connections, and often its own character set, collation, and access-control boundary.

sql
-- PostgreSQL CREATE DATABASE company_db WITH ENCODING = 'UTF8' OWNER = admin TEMPLATE = template0; -- MySQL (identical to CREATE SCHEMA here — see 9.2) CREATE DATABASE IF NOT EXISTS company_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -- SQL Server CREATE DATABASE CompanyDB;

How It Works

The server allocates catalog metadata (an entry in a system table like pg_database or information_schema.SCHEMATA) and the physical storage backing it (data files, log files, or a tablespace). Once created, the database becomes a connection target — clients must actually connect to company_db before they can create tables inside it; you cannot create objects in a database you haven't switched into.

A practical worked example: setting up a fresh environment for a new microservice.

sql
CREATE DATABASE billing_service WITH ENCODING = 'UTF8' OWNER = billing_admin; \c billing_service CREATE TABLE invoices (id SERIAL PRIMARY KEY, amount NUMERIC(10,2));

Edge Cases and Pitfalls

  • Cannot run inside a transaction block on most engines (PostgreSQL explicitly forbids CREATE DATABASE inside BEGIN...COMMIT) because it involves file-system-level operations that aren't easily made transactional.
  • Name collisions: database names must be unique per server instance; re-running without IF NOT EXISTS throws an error.
  • Case sensitivity of database names can differ by operating system (case-insensitive on Windows file systems, case-sensitive on Linux), which occasionally causes cross-environment bugs.
  • Dropping requires no active connections in many engines — DROP DATABASE fails if any session is still connected to it.
  • Costs real disk/memory resources immediately, even before a single table is created — don't create databases speculatively in production.

Key Takeaways / Q&A

Q: Is a database the same thing as a schema? A: Not always — see the CREATE SCHEMA topic. In PostgreSQL/SQL Server/Oracle, a schema is a namespace inside a database. In MySQL, "schema" and "database" are literal synonyms — CREATE SCHEMA and CREATE DATABASE do exactly the same thing.

Q: Can two databases on the same server share data via a plain SQL JOIN? A: Generally no — cross-database queries require special mechanisms (linked servers, dblink, foreign data wrappers, or fully-qualified database.schema.table syntax where the engine supports it), because databases are meant to be isolated units.

Mock Test

  • CREATE DATABASE - Quick Test

    8 questions on CREATE DATABASE.

    8 questions · 8 min · Medium
    Start Mock Test