How to Create a Database in PostgreSQL

1. Syntax to Create a Database

CREATE DATABASE database_name;

What it does:

This command creates a new database named database_name in your PostgreSQL server.

Where to type:

You enter this command inside the psql command line interface after connecting to your PostgreSQL server.

2. Step-by-Step Guide

  1. Open Terminal/Command Prompt: Open the terminal on your computer where PostgreSQL is installed.
  2. Connect to PostgreSQL Server: Type this command and press Enter:
    psql -U postgres

    This logs you into PostgreSQL as the user postgres (the default superuser).

  3. Create the Database: Once connected, type the syntax to create your database:
    CREATE DATABASE database_name;

    Replace database_name with your desired database name. Then press Enter.

  4. Expected Output:
    CREATE DATABASE

    This confirms the database was successfully created.

  5. Verify the Database: To see the list of all databases, type:
    \l

    You will see a list including your new database.

  6. Connect to Your Database: To start using your new database, type:
    \c database_name

    You will see confirmation:

    You are now connected to database "database_name".

Reference: Official PostgreSQL Documentation: CREATE DATABASE

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top