← Back to PostgreSQL Course | Chapter 1: Introduction | Lesson 5 of 7

Creating database

CREATE DATABASE makes a new, separate database on the server.

In this page:

  1. Creating database
Syntax
sql
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

bash
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
  1. Trying to drop the database you are connected to
  2. Forgetting that databases are isolated from each other
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.