DEV Community

Shahid
Shahid

Posted on

PostgreSQL psql Backslash Commands: A Practical Guide

When working with PostgreSQL from the terminal, psql provides special commands that begin with a backslash, such as \l, \dt, and \d. These are called meta-commands or backslash commands; they are processed by the psql client rather than sent to the PostgreSQL server as SQL. postgresql

This guide explains the most useful commands for viewing databases, tables, schemas, users, permissions, query results, and configuration.

Getting started

Connect to PostgreSQL from your terminal:

psql -U postgres -d postgres -h localhost -p 5432
Enter fullscreen mode Exit fullscreen mode

Where:

  • -U postgres specifies the PostgreSQL user.
  • -d postgres specifies the database.
  • -h localhost specifies the server host.
  • -p 5432 specifies the port.

On Linux, you may also connect as the operating-system postgres user:

sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

A successful connection displays a prompt similar to:

postgres=#
Enter fullscreen mode Exit fullscreen mode

The prompt changes when you connect to another database:

myapp_db=#
Enter fullscreen mode Exit fullscreen mode

Help and exiting

Show all psql commands

\?
Enter fullscreen mode Exit fullscreen mode

This displays the complete list of available psql meta-commands.

You can also display help for a category:

\? commands
\? options
\? variables
Enter fullscreen mode Exit fullscreen mode

Get help for SQL commands

\h
Enter fullscreen mode Exit fullscreen mode

Show the syntax for a specific SQL command:

\h CREATE TABLE
\h ALTER USER
\h GRANT
Enter fullscreen mode Exit fullscreen mode

Exit psql

\q
Enter fullscreen mode Exit fullscreen mode

You can also press Ctrl+D on Linux and macOS.

Databases and connections

List all databases

\l
Enter fullscreen mode Exit fullscreen mode

Example output:

   Name    |  Owner   | Encoding | Collate |  Ctype  | Access privileges
-----------+----------+----------+---------+---------+-------------------
 myapp_db  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
Enter fullscreen mode Exit fullscreen mode

Show additional information such as database size:

\l+
Enter fullscreen mode Exit fullscreen mode

Connect to another database

\c myapp_db
Enter fullscreen mode Exit fullscreen mode

You can specify both a database and a user:

\c myapp_db app_user
Enter fullscreen mode Exit fullscreen mode

For a remote database:

\c myapp_db app_user db.example.com 5432
Enter fullscreen mode Exit fullscreen mode

Show the current connection

\conninfo
Enter fullscreen mode Exit fullscreen mode

Example:

You are connected to database "myapp_db" as user "app_user" on host "localhost" at port "5432".
Enter fullscreen mode Exit fullscreen mode

Show or change client encoding

\encoding
Enter fullscreen mode Exit fullscreen mode

Set UTF-8 encoding:

\encoding UTF8
Enter fullscreen mode Exit fullscreen mode

Schemas and tables

List schemas

\dn
Enter fullscreen mode Exit fullscreen mode

Show schemas with descriptions and access privileges:

\dn+
Enter fullscreen mode Exit fullscreen mode

List tables

\dt
Enter fullscreen mode Exit fullscreen mode

This lists tables visible through the current search_path.

List tables in all schemas:

\dt *.*
Enter fullscreen mode Exit fullscreen mode

List tables in a particular schema:

\dt myapp.*
Enter fullscreen mode Exit fullscreen mode

Show extra information, such as table size:

\dt+
Enter fullscreen mode Exit fullscreen mode

Describe a table

\d users
Enter fullscreen mode Exit fullscreen mode

This usually shows:

  • Column names.
  • Data types.
  • Nullable status.
  • Default values.
  • Indexes.
  • Primary keys.
  • Foreign keys.
  • Constraints.

For a table in a specific schema:

\d myapp.users
Enter fullscreen mode Exit fullscreen mode

Show more detailed information:

\d+ myapp.users
Enter fullscreen mode Exit fullscreen mode

Example output:

                                      Table "myapp.users"
 Column |           Type           | Collation | Nullable |              Default
--------+--------------------------+-----------+----------+-----------------------------------
 id     | bigint                   |           | not null | generated by default as identity
 email  | text                     |           | not null |
 name   | text                     |           |          |
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
    "users_email_key" UNIQUE CONSTRAINT, btree (email)
Enter fullscreen mode Exit fullscreen mode

List all relations

\d
Enter fullscreen mode Exit fullscreen mode

This can show tables, views, sequences, and other relation-like objects.

Views, indexes, sequences, and functions

List views

\dv
\dv+
Enter fullscreen mode Exit fullscreen mode

Describe a view:

\d myapp.active_users
Enter fullscreen mode Exit fullscreen mode

Show the SQL definition of a view:

\sv myapp.active_users
Enter fullscreen mode Exit fullscreen mode

List materialized views

\dm
\dm+
Enter fullscreen mode Exit fullscreen mode

List indexes

\di
\di+
Enter fullscreen mode Exit fullscreen mode

Describe indexes for a table:

\d myapp.users
Enter fullscreen mode Exit fullscreen mode

List sequences

\ds
\ds+
Enter fullscreen mode Exit fullscreen mode

List functions

\df
\df+
Enter fullscreen mode Exit fullscreen mode

List functions matching a pattern:

\df *email*
Enter fullscreen mode Exit fullscreen mode

Describe a particular function:

\df myapp.calculate_total
Enter fullscreen mode Exit fullscreen mode

Users, roles, and permissions

List users and roles

\du
Enter fullscreen mode Exit fullscreen mode

Show additional role information:

\du+
Enter fullscreen mode Exit fullscreen mode

Example output:

 Role name  | Attributes | Member of
------------+------------+-----------
 app_user   |            | {}
 postgres   | Superuser  | {}
Enter fullscreen mode Exit fullscreen mode

Show table privileges

\dp
Enter fullscreen mode Exit fullscreen mode

You can also use:

\z
Enter fullscreen mode Exit fullscreen mode

Show privileges for a specific table:

\dp myapp.users
Enter fullscreen mode Exit fullscreen mode

Example:

                              Access privileges
 Schema | Name  | Type  | Access privileges
--------+-------+-------+-------------------
 myapp  | users | table | app_user=arwdDxt/app_owner
Enter fullscreen mode Exit fullscreen mode

The privilege letters generally represent:

  • r: SELECT
  • a: INSERT
  • w: UPDATE
  • d: DELETE
  • D: TRUNCATE
  • x: REFERENCES
  • t: TRIGGER

Show object descriptions

\dd
Enter fullscreen mode Exit fullscreen mode

Show the description of a specific object:

\dd myapp.users
Enter fullscreen mode Exit fullscreen mode

Viewing table data

Backslash commands show metadata. To view actual rows, use SQL.

Display all rows:

SELECT * FROM myapp.users;
Enter fullscreen mode Exit fullscreen mode

Display selected columns:

SELECT id, email, name
FROM myapp.users;
Enter fullscreen mode Exit fullscreen mode

Limit the result:

SELECT *
FROM myapp.users
LIMIT 10;
Enter fullscreen mode Exit fullscreen mode

Sort the result:

SELECT id, email, created_at
FROM myapp.users
ORDER BY created_at DESC
LIMIT 10;
Enter fullscreen mode Exit fullscreen mode

Count rows:

SELECT COUNT(*)
FROM myapp.users;
Enter fullscreen mode Exit fullscreen mode

The SQL statement must end with a semicolon:

SELECT * FROM myapp.users;
Enter fullscreen mode Exit fullscreen mode

A psql meta-command such as \dt normally does not require a semicolon:

\dt
Enter fullscreen mode Exit fullscreen mode

Improving query output

Toggle expanded output

Normal output is displayed horizontally:

SELECT * FROM myapp.users LIMIT 1;
Enter fullscreen mode Exit fullscreen mode

For wide rows, use expanded output:

\x
SELECT * FROM myapp.users LIMIT 1;
Enter fullscreen mode Exit fullscreen mode

Toggle it back:

\x
Enter fullscreen mode Exit fullscreen mode

You can let psql decide automatically:

\x auto
Enter fullscreen mode Exit fullscreen mode

Show query execution time

\timing on
Enter fullscreen mode Exit fullscreen mode

Now each query displays its execution time.

Disable it with:

\timing off
Enter fullscreen mode Exit fullscreen mode

Show only rows without headers

\t
Enter fullscreen mode Exit fullscreen mode

Run a query:

SELECT email FROM myapp.users;
Enter fullscreen mode Exit fullscreen mode

Toggle tuple-only output off:

\t
Enter fullscreen mode Exit fullscreen mode

Change the output format

Use expanded output:

\x on
Enter fullscreen mode Exit fullscreen mode

Use unaligned output, which is useful for scripts:

\a
Enter fullscreen mode Exit fullscreen mode

Set a particular output format:

\pset format aligned
\pset format unaligned
\pset format csv
Enter fullscreen mode Exit fullscreen mode

Set a display value for NULL:

\pset null '(none)'
Enter fullscreen mode Exit fullscreen mode

Running SQL files

Execute a SQL file

\i /path/to/schema.sql
Enter fullscreen mode Exit fullscreen mode

Example:

\i /home/admin/migrations/001_create_users.sql
Enter fullscreen mode Exit fullscreen mode

For a file relative to the current script:

\ir migrations/001_create_users.sql
Enter fullscreen mode Exit fullscreen mode

This is useful for running database setup scripts and migrations.

Redirect output to a file

\o query_output.txt
SELECT * FROM myapp.users;
\o
Enter fullscreen mode Exit fullscreen mode

The first \o starts writing output to the file. The second \o returns output to the terminal.

Export data with \copy

Export a query result to CSV:

\copy (
  SELECT id, email, name
  FROM myapp.users
) TO '/tmp/users.csv' WITH CSV HEADER
Enter fullscreen mode Exit fullscreen mode

Import CSV data into a table:

\copy myapp.users(email, name)
FROM '/tmp/users.csv'
WITH CSV HEADER
Enter fullscreen mode Exit fullscreen mode

\copy reads and writes files on the machine running the psql client, which is different from server-side SQL COPY.

Query history and editing

Show command history

\history
Enter fullscreen mode Exit fullscreen mode

You can also use:

\s
Enter fullscreen mode Exit fullscreen mode

Save history to a file:

\s /tmp/psql-history.txt
Enter fullscreen mode Exit fullscreen mode

Clear the current query buffer

\r
Enter fullscreen mode Exit fullscreen mode

This is useful if you started writing a SQL command but no longer want to execute it.

Edit the current query

\e
Enter fullscreen mode Exit fullscreen mode

This opens the current query buffer in your configured text editor.

You can set an editor before launching psql:

export EDITOR=nano
psql -U postgres -d myapp_db
Enter fullscreen mode Exit fullscreen mode

Execute the query buffer

\g
Enter fullscreen mode Exit fullscreen mode

This runs the SQL currently stored in the query buffer.

Shell and file-system commands

Run an operating-system command

On Linux or macOS:

\! pwd
\! ls
Enter fullscreen mode Exit fullscreen mode

On Windows:

\! cd
\! dir
Enter fullscreen mode Exit fullscreen mode

Change the client working directory

\cd /tmp
Enter fullscreen mode Exit fullscreen mode

Check the current client working directory:

\! pwd
Enter fullscreen mode Exit fullscreen mode

This affects local file commands such as \i and \copy.

Variables and prompts

List psql variables

\set
Enter fullscreen mode Exit fullscreen mode

Set a variable:

\set environment 'development'
Enter fullscreen mode Exit fullscreen mode

Display it:

\echo :environment
Enter fullscreen mode Exit fullscreen mode

Unset it:

\unset environment
Enter fullscreen mode Exit fullscreen mode

Variables can be used in SQL:

\set user_id 10

SELECT *
FROM myapp.users
WHERE id = :user_id;
Enter fullscreen mode Exit fullscreen mode

For string values, quote the value appropriately:

\set email '''alice@example.com'''

SELECT *
FROM myapp.users
WHERE email = :email;
Enter fullscreen mode Exit fullscreen mode

For application scripts, use parameterized queries rather than manually constructing SQL strings.

Automatic query execution

Repeat a query

Run a query every five seconds:

SELECT COUNT(*) FROM myapp.users
\watch 5
Enter fullscreen mode Exit fullscreen mode

Stop the repeating query with:

Ctrl+C
Enter fullscreen mode Exit fullscreen mode

Execute query output as SQL

SELECT 'CREATE TABLE test(id integer);'
\gexec
Enter fullscreen mode Exit fullscreen mode

\gexec executes each value returned by the query as SQL. Use it carefully, especially in production.

A complete inspection workflow

The following sequence is useful when investigating a database:

-- Show the current connection
\conninfo

-- List all databases
\l

-- Connect to the application database
\c myapp_db

-- List schemas
\dn+

-- List tables in the application schema
\dt myapp.*

-- Describe a table
\d+ myapp.users

-- List views
\dv myapp.*

-- List indexes
\di myapp.*

-- List sequences
\ds myapp.*

-- List users and roles
\du+

-- Show table privileges
\dp myapp.users

-- Show query execution time
\timing on

-- Inspect some rows
SELECT *
FROM myapp.users
LIMIT 10;
Enter fullscreen mode Exit fullscreen mode

Common mistakes

Using a backslash command outside psql

This will not work directly in a normal shell:

\dt
Enter fullscreen mode Exit fullscreen mode

Start psql first:

psql -U app_user -d myapp_db
Enter fullscreen mode Exit fullscreen mode

Then run:

\dt
Enter fullscreen mode Exit fullscreen mode

Adding a semicolon incorrectly

Use:

\dt
Enter fullscreen mode Exit fullscreen mode

Not:

\dt;
Enter fullscreen mode Exit fullscreen mode

SQL commands, however, require a semicolon:

SELECT * FROM myapp.users;
Enter fullscreen mode Exit fullscreen mode

Expecting tables from another database

Tables belong to a specific database. First connect to the correct database:

\c myapp_db
\dt
Enter fullscreen mode Exit fullscreen mode

Not seeing a table

The table may be in another schema. Try:

\dt *.*
Enter fullscreen mode Exit fullscreen mode

Or describe it with its full name:

\d myapp.users
Enter fullscreen mode Exit fullscreen mode

You may also lack the privileges required to see or access it.

Quick reference

Command Purpose Example
\l List databases \l
\c Connect to a database \c myapp_db
\conninfo Show connection details \conninfo
\dn List schemas \dn
\dt List tables \dt myapp.*
\d Describe an object \d myapp.users
\dv List views \dv
\di List indexes \di
\ds List sequences \ds
\df List functions \df
\du List users and roles \du
\dp or \z Show privileges \dp myapp.users
\x Toggle expanded output \x auto
\timing Toggle query timing \timing on
\i Execute a SQL file \i setup.sql
\copy Import or export CSV \copy users TO 'users.csv' CSV
\e Edit the query buffer \e
\r Clear the query buffer \r
\? Show psql help \?
\h Show SQL help \h CREATE TABLE
\q Exit psql \q

The most important commands to remember are:

\l
\c database_name
\dn
\dt
\d table_name
\du
\dp
\conninfo
\?
\q
Enter fullscreen mode Exit fullscreen mode

For the authoritative and version-specific list, run \? inside psql; available commands can vary slightly between PostgreSQL client versions. postgresql

Top comments (0)