How to Create Database and Table Using PSQL (Step-by-Step Beginner Guide)

  

How to Create Database and Table Using PSQL (Step-by-Step Beginner Guide)

How to Create Database & Table Using PSQL | PostgreSQL Command Line Tutorial (Beginner Guide)


If you are starting your journey in SQL and data analytics, learning how to use the PostgreSQL command line tool (PSQL) is a must. In this guide, you will learn how to create a database and tables using simple commands in PostgreSQL.


 What is PSQL?

PSQL is the command-line interface for PostgreSQL, a powerful open-source relational database system. It allows you to interact with your database using SQL commands.


Step 1: Open PSQL

After installing PostgreSQL, open the PSQL terminal:

  • On Windows: Search for SQL Shell (psql)

  • Enter your:

    • Server

    • Database

    • Username

    • Password


 Step 2: Create a Database

Use the following command to create a new database:

CREATE DATABASE salesdb;

👉 This will create a database named salesdb


 Step 3: Connect to the Database

Now connect to your newly created database:

\c salesdb

👉 You are now working inside salesdb


 Step 4: Create a Table

Now create a table using SQL:

CREATE TABLE sales_data (
    id SERIAL PRIMARY KEY,
    product_name VARCHAR(50) NOT NULL,
    quantity INTEGER,
    price NUMERIC(10,2)
);

👉 This creates a table with:

  • Auto-increment ID

  • Product name

  • Quantity

  • Price


 Step 5: Verify Table Creation

To check your table, run:

\dt

👉 This will display all tables in the current database


⚠️ Common Mistakes

  • Running \c in pgAdmin (it only works in PSQL)

  • Creating tables in the wrong database

  • Forgetting to use NOT NULL constraints


🧠 Pro Tips

  • Always use UPPERCASE for SQL keywords

  • Use snake_case for table and column names

  • Keep your queries clean and readable


🎯 Conclusion

Creating databases and tables using PSQL is a fundamental skill for anyone learning PostgreSQL. Once you understand these basics, you can start inserting data and running queries to analyze real-world datasets.


👉 In the next tutorial, we will learn how to insert data and run SELECT queries using PSQL.

Watch Full Tutorial Here : How to Create Database and Table Using PSQL

Comments