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
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
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
DROP DATABASE store_db; -- Avoid an error if it might not exist DROP DATABASE IF EXISTS store_db;
A database is the top-level container: it holds schemas, tables, views, functions, and users/permissions specific to it.
CREATE DATABASE name;andDROP 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 DATABASEin a real environment.Many teams restrict
DROP DATABASEprivileges to administrators only, precisely because of how destructive and irreversible it is.