DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on

PostgreSQL Commands – Step-by-Step Guide

PostgreSQL is an open-source relational database management system (RDBMS) used to store, manage, and retrieve data.

Step 1: Check whether PostgreSQL is installed

Run:

psql --version

Step 2: If PostgreSQL is not installed

On Linux Mint / Ubuntu, install it using:

sudo apt update
sudo apt install postgresql

Step 3: Switch to PostgreSQL user

sudo -i -u postgres

Then open PostgreSQL:

psql

Purpose: Switches the current terminal session to the postgres Linux user.

sudo --> Executes a command with elevated privileges
-i --> Starts an interactive login shell
-u --> Specifies the user
postgres --> PostgreSQL system user

Step 4: Check PostgreSQL version

Select version();

Step 5: Get Help

\h

Purpose: Displays help about SQL commands in PostgreSQL.

Step 6: List All Databases

\l

Purpose: Displays all databases available in the PostgreSQL server.

Step 7: Create a Database

CREATE DATABASE movie;

Purpose: Creates a new database named movie.
Note: SQL commands should end with a semicolon (;).

Step 8: Quit the PostgreSQL Terminal

\q

Step 9: Connect to a Database

\c movie

Purpose: Connects to the database named movie.

Step 10: Clear the Terminal Screen

Ctrl + l

Step 11: Display Tables

\d

Purpose: Displays the tables, views, sequences, and other relations in the current database.

Step 12: Display Table Structure

\d movie;

Top comments (0)