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
.sqlfiles
πͺ 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)
-
Click Start Menu
-
Search:
SQL Shell (psql) -
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 postgresIf it does not work, use full path:
"C:\Program Files\PostgreSQL\16\bin\psql.exe" -U postgres(Version folder may be 15, 14, etc.)
Method 3: Add psql to PATH (Recommended)
So you can run psql from anywhere:
Steps:
- Copy this path:
C:\Program Files\PostgreSQL\16\bin
- Go to:
Control Panel β System β Advanced System Settings β Environment Variables
- Edit Path
- Add new entry:
C:\Program Files\PostgreSQL\16\bin
for more details, see Fix 'psql' is not recognized on Windows
- Restart CMD
Now just type:
psql -U postgresπ 3. Login Examples
Login as postgres user:
psql -U postgresLogin with database:
psql -U postgres -d university_dbLogin 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 postgresIf 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 mydatabase3. Login with a new user
First create a user:
CREATE ROLE student_user WITH LOGIN PASSWORD '1234';Then login:
psql -U student_user -d mydatabaseOr:
psql -U student_user -h localhost -d mydatabase4. 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