How to exit psql
Leave the PostgreSQL psql shell with \q, quit, exit or Ctrl+D, and what to do when psql ignores you because a query or pager is still open.
Quick Answer
Type \q and press Enter:
postgres=# \q
On psql 11 and later, quit and exit also work, and Ctrl+D works everywhere.
Every way to leave psql
| Command | Works in | Notes |
|---|---|---|
\q | Every version | The meta-command. Works on an empty or half-typed line, but not inside an open quote. |
quit / exit | psql 11+ | Only on their own line, with nothing else typed before them. |
Ctrl+D | Every version | Sends end-of-input. Works on an empty prompt. |
Ctrl+C | Every version | Does not exit. Cancels the running query or clears the current line. |
\q is the one to remember. It's what psql's own help text points to, and it works the same in old and new versions.
In VisuaLeaf
Skip the psql prompt entirely
Most of the traps below, an open quote, an unfinished statement or the pager, come from psql reading your input line by line. VisuaLeaf's SQL editor has no prompt to get stuck in:
- Open the connection and press Ctrl/Cmd + Shift + S for the SQL editor.
- Write the query across as many lines as you like. Press F5 to run everything, or highlight part of it and press F9.
- Results open in a paged grid, so there's no pager to quit, and several statements give you one result tab each.

See the SQL Editor docs.
psql won't quit: check the prompt
The character before # tells you what psql is waiting for, and that decides which exit works:
| Prompt | Waiting for | What quits |
|---|---|---|
postgres=# | A new command | \q, quit, exit, Ctrl+D |
postgres-# | The rest of a statement (usually a ;) | \q or Ctrl+D. exit only prints a hint |
postgres(# | A closing parenthesis | \q or Ctrl+D |
postgres'# | A closing single quote | Only Ctrl+D. \q becomes part of the string |
postgres=# SELECT 1
postgres-# exit
Use \q to quit.
To stay in psql instead, press Ctrl+C: it throws away the half-typed statement and gives you the normal prompt back.
What we found testing this
We typed each of these into an interactive psql 16.14 session. Most guides list the commands, but not these edge cases:
- Inside an open quote,
\qdoesn't quit. psql treats it as text and answers with a different hint than usual:
If you then close the quote and run the query,postgres=# select 'abc postgres'# \q Use control-D to quit.\qcomes back as part of the string. Ctrl+D is the only key that exits here. exit;with a semicolon still quits on an empty line, so you don't have to remember to leave it off.- Ctrl+D prints
\qfor you on the prompt line before exiting, so a terminal recording shows\qeven when nobody typed it. - Ctrl+C never exits, on any OS. During a query it prints
Cancel request sentandERROR: canceling statement due to user request. On a half-typed line it clears the buffer. Some popular guides say Ctrl+C quits psql on Windows; that's not what psql does.
Stuck in the result pager (the screen shows : or (END))
Long results open in a pager, usually less. psql is still running underneath, but your keystrokes go to the pager until you leave it. Press q to close the pager, then \q to leave psql.
If the pager gets in your way, turn it off for the session or permanently:
\pset pager off -- this session
-- or add the line above to ~/.psqlrc to make it permanent
Exit after running one command
If you only need one query, don't open the interactive shell at all. -c runs the command and exits, and -f runs a file:
psql -U postgres -d shop -c "SELECT count(*) FROM orders;"
psql -U postgres -d shop -f migrate.sql
A query that's still running keeps psql busy. Press Ctrl+C once to cancel it, then \q. If it won't cancel, another session can stop it with pg_cancel_backend(pid).
Frequently Asked Questions
What is the command to quit psql?
\q. On psql 11 and newer you can also type quit or exit on an empty line, or press Ctrl+D.
Why does typing exit in psql not work?
Either your psql is older than version 11, or you typed it while a statement was unfinished (the prompt ends in -#). Use \q, which works in both cases. If the prompt ends in '# you are inside a quote, and only Ctrl+D exits.
Does Ctrl+C exit psql?
No. Ctrl+C cancels the running query or clears what you have typed. Use \q or Ctrl+D to exit.
Does quitting psql roll back my transaction?
Yes. If you ran BEGIN and quit without COMMIT, the server rolls the transaction back when the connection closes.