Lesson 1of 2

How to Access and Use psql in Windows | PostgreSQL Command-Line Tool

Complete guide to accessing and using psql in Windows. Learn multiple methods to open psql, configure PATH variables, fix 'psql not recognized' errors, and execute SQL commands from the terminal.


What is psql in PostgreSQL?

psql is the interactive command-line tool for working with PostgreSQL.

It allows you to:

  • Execute SQL queries
  • Manage databases and users
  • Run scripts
  • View/query data quickly from the terminal

Think of psql as a terminal interface (client) to communicate with the PostgreSQL server.


Why do we use psql?

We use psql because it is:

  • ⚑ Fast – no GUI needed
  • 🧰 Powerful – supports SQL + special commands
  • πŸ”§ Administrative – manage users, roles, databases
  • πŸ“œ Scriptable – automate tasks using .sql files

πŸͺŸ How to Access psql in Windows

βœ… 1. Install PostgreSQL (if not installed)

Download from:

During installation:

  • Set password for postgres user
  • Keep default port 5432
  • Make sure Command Line Tools (psql) is selected

βš™οΈ 2. Open psql in Windows

Method 1: Using SQL Shell (psql) (Easiest)

  1. Click Start Menu

  2. Search:

    SQL Shell (psql)
    
  3. Open it

You will see prompts like:

Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password:

πŸ‘‰ Press Enter for defaults and enter password.


Method 2: Using Command Prompt (CMD)

Step 1: Open CMD

Press:

Win + R β†’ type cmd β†’ Enter

Step 2: Run psql

psql -U postgres

If it does not work, use full path:

"C:\Program Files\PostgreSQL\16\bin\psql.exe" -U postgres

(Version folder may be 15, 14, etc.)


So you can run psql from anywhere:

Steps:

  1. Copy this path:
C:\Program Files\PostgreSQL\16\bin
  1. Go to:
Control Panel β†’ System β†’ Advanced System Settings β†’ Environment Variables
  1. Edit Path
  2. Add new entry:
C:\Program Files\PostgreSQL\16\bin

for more details, see Fix 'psql' is not recognized on Windows

  1. Restart CMD

Now just type:

psql -U postgres

πŸ” 3. Login Examples

Login as postgres user:

psql -U postgres

Login with database:

psql -U postgres -d university_db

Login with host & port:

psql -U postgres -h localhost -p 5432

πŸ“‚ 4. Useful First Commands after login

\l        -- list databases
\c postgres   -- connect database
\dt       -- list tables
\du       -- list users
\q        -- quit

⚠️ Common Problems & Fixes

❌ β€œpsql is not recognized”

βœ” Fix: Add PostgreSQL bin folder to PATH


❌ Password error

βœ” Fix:

  • Check password used during installation
  • Reset postgres password if needed

❌ Connection failed

βœ” Fix:

  • Ensure PostgreSQL service is running:
Services β†’ PostgreSQL β†’ Start

How to Login into psql

1. Login with postgres (default superuser)

psql -U postgres

If PostgreSQL is running locally:

psql -U postgres -h localhost -p 5432

πŸ‘‰ You will be prompted for a password.


2. Login into a specific database

psql -U postgres -d mydatabase

3. Login with a new user

First create a user:

CREATE ROLE student_user WITH LOGIN PASSWORD '1234';

Then login:

psql -U student_user -d mydatabase

Or:

psql -U student_user -h localhost -d mydatabase

4. Login using connection string

psql "postgresql://student_user:1234@localhost:5432/mydatabase"

πŸ“˜ psql Backslash Commands (Meta-Commands) with Examples

In psql (PostgreSQL interactive terminal), backslash commands (\) are used to manage and explore databases.

πŸ‘‰ Key point:

  • Executed by psql client (not SQL server)
  • Start with \
  • Very useful for learning and administration

πŸ” 1. Essential Discovery Commands (with Examples)

πŸ“Œ \l β†’ List all databases

\l

πŸ‘‰ Shows all databases on the PostgreSQL server


πŸ“Œ \dn β†’ List schemas

\dn

πŸ‘‰ Example output:

  • public
  • information_schema

πŸ“Œ \dt β†’ List tables

\dt

πŸ‘‰ Example:

List of relations
Schema | Name     | Type  | Owner
-------+----------+-------+-------
public | students | table | postgres

πŸ“Œ \di β†’ List indexes

\di

πŸ‘‰ Shows indexes created on tables


πŸ“Œ \dv β†’ List views

\dv

πŸ‘‰ Displays all views in current database


πŸ“Œ \df β†’ List functions

\df

πŸ‘‰ Shows stored functions


πŸ“Œ \du β†’ List users/roles

\du

πŸ‘‰ Example:

Role name | Attributes
----------+----------------
postgres  | Superuser
student   | Create DB

🧭 2. Navigation & Connection Commands

πŸ“Œ \c database_name β†’ Connect to database

\c university_db

πŸ‘‰ Output:

You are now connected to database "university_db"

πŸ“Œ \conninfo β†’ Show connection details

\conninfo

πŸ‘‰ Example:

You are connected to database "university_db" as user "postgres" on host "localhost" at port "5432"

πŸ“Œ \q β†’ Quit psql

\q

πŸ‘‰ Exits PostgreSQL terminal


πŸ“Š 3. Inspecting Objects (Very Important for Students)

πŸ“Œ \d table_name β†’ Describe table

\d students

πŸ‘‰ Example output:

Column | Type         | Constraints
-------+-------------+-------------
id     | integer      | primary key
name   | varchar(50)  |

πŸ“Œ \d+ table_name β†’ Detailed table info

\d+ students

πŸ‘‰ Shows:

  • Column details
  • Indexes
  • Table size
  • Comments

πŸ–₯️ 4. System & Output Commands

πŸ“Œ \x β†’ Expanded view toggle

\x
SELECT * FROM students;

πŸ‘‰ Output becomes vertical (useful for many columns):

id   : 1
name : Ali

πŸ“Œ \timing β†’ Show query execution time

\timing
SELECT * FROM students;

πŸ‘‰ Output:

Time: 1.234 ms

πŸ“Œ \i filename.sql β†’ Run SQL file

\i school.sql

πŸ‘‰ Executes all SQL inside file:

CREATE TABLE students (...);
INSERT INTO students VALUES (...);

πŸ“Œ \! command β†’ Run OS command

\! ls

πŸ‘‰ Example output:

school.sql
data.sql

Other example:

\! pwd

❓ 5. Help Commands (Must Know)

πŸ“Œ \? β†’ All psql commands

\?

πŸ‘‰ Shows full list of meta-commands


πŸ“Œ \h β†’ SQL help

\h

πŸ“Œ \h command β†’ Specific SQL help

\h CREATE TABLE

πŸ‘‰ Shows syntax:

CREATE TABLE table_name (
    column_name data_type
);

πŸ§ͺ Examples

\l                          -- list databases
\c university_db            -- connect database
\dn                         -- show schemas
\dt                         -- show tables
 
\d students                -- describe table
SELECT * FROM students;
 
\x                          -- expanded view
\timing                     -- enable timing
 
\! ls                       -- check files
\i insert_data.sql         -- run SQL file

⭐ Summary

βœ” \ commands = psql meta-commands βœ” Used for:

  • Database navigation
  • Table inspection
  • System control
  • Debugging & learning