HOWTO · PostgreSQL
How to Run PostgreSQL Queries in PSQL
Learn how to run SQL interactively, select a PostgreSQL database, use psql single-line mode, and execute a SQL file.
On this page
Use psql, PostgreSQL’s terminal client, to run SQL interactively, send a query to a selected database, or execute statements from a .sql file. Unlike pgAdmin, psql runs in a terminal and accepts SQL statements as well as its own backslash commands. These examples assume a reachable PostgreSQL server and a role with permission to connect to the selected database.
Run SQL Statements in an Interactive psql Session
Start an interactive session with psql -U postgres -d postgres, replacing postgres with a role and database available on your installation. At the prompt, enter a SQL statement and finish it with a semicolon. The following temporary-table example can be repeated in the same session. It clears the session-local example table before inserting the two rows, so the displayed result stays the same each time.
CREATE TEMP TABLE IF NOT EXISTS bag (id integer PRIMARY KEY, model text NOT NULL);
TRUNCATE bag;
INSERT INTO bag (id, model) VALUES (1, 'Adidas'), (2, 'Jale') ON CONFLICT (id) DO UPDATE SET model = EXCLUDED.model;
SELECT id, model FROM bag ORDER BY id;
The SELECT returns these columns and rows:
| id | model |
|---|---|
| 1 | Adidas |
| 2 | Jale |
In an interactive session, pressing Enter moves to the next input line; it does not by itself finish SQL. If a statement has no terminating semicolon, psql keeps accepting input, commonly showing a continuation prompt. Add the semicolon to complete the statement. If you started the wrong statement, use \r to clear the query buffer and begin again. If you accidentally append another command before terminating the first statement, psql treats the input as one SQL statement, which may produce a syntax error.
Connect to the Target Database and Run a Query
List databases visible to your role with \l or \l+ (\l+ includes additional details). You can also query the PostgreSQL catalog with SELECT datname FROM pg_database ORDER BY datname;. The catalog lists databases, but whether your role can connect to one depends on permissions and server configuration.
Inside psql, connect to a database with \c postgres; replace the name with the target database. Use \dt to list tables in the current search path. To inspect other relation types, use \d, which also lists views, sequences, and other supported relation types. To connect at startup, use the -d option: psql -U postgres -d postgres.
You can pass a query with -c, for example: psql -U postgres -d postgres -c "SELECT datname FROM pg_database ORDER BY datname;". This catalog query works in a fresh psql session and does not depend on tables created earlier. Shell quoting rules vary. In a POSIX-compatible shell, double quotes group the SQL as one command-line argument; they are shell syntax, not SQL quotes. If a query contains SQL string literals, the shell passes their single quotes through inside this double-quoted argument. In PowerShell or Command Prompt, quoting and escaping can differ; adjust the command for that shell. A semicolon terminates the SQL statement, but a final semicolon is optional for a single statement passed with -c.
Run a One-Line Query with Single-Line Mode
Start psql with -S or --single-line to enable single-line mode, such as psql -U postgres -d postgres -S. In this mode, a newline submits the current input line as a query, so enter one SQL statement per line. For example, enter SELECT datname FROM pg_database ORDER BY datname and press Enter. This mode is intended for single-line input; use the normal interactive mode and semicolon terminators for SQL statements that span multiple lines.
Execute SQL from a File with psql
To execute a SQL file when starting the client, use psql -U postgres -d postgres -f "/path/to/query.sql". Replace the placeholder with the file’s actual path. Quote paths that contain spaces according to your shell’s rules; path separators also depend on the operating system and shell.
From an existing interactive session, run the same file with \i '/path/to/query.sql'. The file must be readable from the environment where psql is running. Both methods send the file’s SQL to the selected database, so confirm the database and role before running statements that change data.