Get Started Free

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.

Updated November 9, 2026 ยท Tested on PostgreSQL 16.14, psql 16.14

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

CommandWorks inNotes
\qEvery versionThe meta-command. Works on an empty or half-typed line, but not inside an open quote.
quit / exitpsql 11+Only on their own line, with nothing else typed before them.
Ctrl+DEvery versionSends end-of-input. Works on an empty prompt.
Ctrl+CEvery versionDoes 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:

  1. Open the connection and press Ctrl/Cmd + Shift + S for the SQL editor.
  2. Write the query across as many lines as you like. Press F5 to run everything, or highlight part of it and press F9.
  3. Results open in a paged grid, so there's no pager to quit, and several statements give you one result tab each.
SQL editor on a PostgreSQL table with a breadcrumb, a Query Builder and SQL toggle, Save, History and Run buttons above a multi-line query
A multi-line query in the SQL editor.

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:

PromptWaiting forWhat 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 quoteOnly 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, \q doesn't quit. psql treats it as text and answers with a different hint than usual:
    postgres=# select 'abc
    postgres'# \q
    Use control-D to quit.
    If you then close the quote and run the query, \q comes 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 \q for you on the prompt line before exiting, so a terminal recording shows \q even when nobody typed it.
  • Ctrl+C never exits, on any OS. During a query it prints Cancel request sent and ERROR: 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.

VisuaLeaf

Skip the psql shortcuts with VisuaLeaf

VisuaLeaf gives you a SQL editor with results in a visual grid, a tree of your databases, schemas and tables, and a connection manager, so you spend less time remembering meta-commands.