Creating database
CREATE DATABASE makes a new, separate database on the server.
In this page:
Syntax
CREATE DATABASE database_name;
Creating database
CREATE DATABASE name creates a database from the template1 template. Options include OWNER, ENCODING and TEMPLATE.
Connect to it with \c name or psql -d name. DROP DATABASE removes it, and you cannot drop a database you are connected to.
Each database has its own schemas and tables.
Note:
From the shell you can also run createdb name.
Example: Creating database
postgres=# CREATE DATABASE shop OWNER appuser ENCODING 'UTF8';
CREATE DATABASE
postgres=# \c shop
You are now connected to database "shop" as user "postgres".
shop=# SELECT current_database();
current_database
------------------
shop
(1 row)
postgres=# DROP DATABASE shop;
DROP DATABASE
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Trying to drop the database you are connected to
- Forgetting that databases are isolated from each other
- Using uppercase names that then need quotes
Chapter Summary
- CREATE DATABASE creates a database
- \c connects to it
- DROP DATABASE removes it
- Databases are isolated from each other
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: