SQLCREATE DATABASE

CREATE DATABASE

Before you can create tables, you need somewhere to put them. A single running database server (also called an "instance") can host many separate databases, each with its own set of tables, views, users, and permissions, isolated from the others. CREATE DATABASE is the statement that adds a new one.

Basic syntax

Creating a database

SQL
CREATE DATABASE store_db;

That single line is enough in every major dialect. Once it succeeds, you connect to store_db specifically (with \c store_db in psql, or by changing the connection's database in your client) before creating tables inside it — tables always belong to exactly one database.

One server, many databases

It's common for a single database server to host several independent databases at once — for example, separate databases for a production application, a staging environment, and an internal analytics tool, all running on the same server instance. Each database is its own isolated namespace: a table called users in one database has no relationship to a table called users in another, and (depending on configuration) a query against one database typically cannot directly join against tables in another.

Multiple isolated databases on one server

SQL
CREATE DATABASE app_production;
CREATE DATABASE app_staging;
CREATE DATABASE analytics;
-- Three separate, isolated databases, one server
Listing existing databases

Dialect

Command

PostgreSQL (psql)

\l (or SELECT datname FROM pg_database;)

MySQL

SHOW DATABASES;

SQL Server

SELECT name FROM sys.databases;

Dropping a database

Deleting a database entirely

SQL
DROP DATABASE store_db;

-- Avoid an error if it might not exist
DROP DATABASE IF EXISTS store_db;
Warning
`DROP DATABASE` is **irreversible**. It deletes the database and every table, view, row, and index inside it — there is no undo short of restoring from a backup. Most database systems refuse to drop a database while any client is actively connected to it, which is a useful safety net, but it is not a substitute for double-checking which database you're connected to and which environment (production vs. staging) you're running the command against.
  • A database is the top-level container: it holds schemas, tables, views, functions, and users/permissions specific to it.

  • CREATE DATABASE name; and DROP DATABASE name; are the two ends of its lifecycle.

  • Always confirm which server and which database your session is connected to before running a DROP DATABASE in a real environment.

  • Many teams restrict DROP DATABASE privileges to administrators only, precisely because of how destructive and irreversible it is.