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.
psql is the interactive command-line tool for working with PostgreSQL.
It allows you to:
Think of psql as a terminal interface (client) to communicate with the PostgreSQL server.
psql?We use psql because it is:
.sql filespsql in WindowsDownload from:
During installation:
Search:
SQL Shell (psql)
You will see prompts like:
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password:
👉 Press Enter for defaults and enter password.
Press:
Win + R → type cmd → Enter
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:
C:\Program Files\PostgreSQL\16\bin
Control Panel → System → Advanced System Settings → Environment Variables
C:\Program Files\PostgreSQL\16\bin
for more details, see Fix ‘psql’ is not recognized on Windows
Now just type:
psql -U postgres
psql -U postgres
psql -U postgres -d university_db
psql -U postgres -h localhost -p 5432
\l -- list databases
\c postgres -- connect database
\dt -- list tables
\du -- list users
\q -- quit
✔ Fix: Add PostgreSQL bin folder to PATH
✔ Fix:
✔ Fix:
Services → PostgreSQL → Start
psqlpsql -U postgres
If PostgreSQL is running locally:
psql -U postgres -h localhost -p 5432
👉 You will be prompted for a password.
psql -U postgres -d mydatabase
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
psql "postgresql://student_user:1234@localhost:5432/mydatabase"
psql Backslash Commands (Meta-Commands) with ExamplesIn psql (PostgreSQL interactive terminal), backslash commands (\) are used to manage and explore databases.
👉 Key point:
\\l → List all databases\l
👉 Shows all databases on the PostgreSQL server
\dn → List schemas\dn
👉 Example output:
\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
\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
\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:
\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
\? → 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
);
\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
✔ \ commands = psql meta-commands
✔ Used for: