Skip to main content
0 reactions

How to Exit psql

Featured card titled Exit psql, with a terminal window listing four ticked ways to quit: \q, exit, quit and Control-D.
Four ways to exit psql and get back to Terminal.

psql keeps you at its postgres=# prompt until you tell it to stop. Here’s every way back to the Terminal prompt, and what to do when the prompt won’t change back.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxPlain text
\q
  • \q: quits psql. It’s one of psql’s own backslash commands, so it needs no semicolon.
  • exit or quit: do the same since PostgreSQL 11 (opens in a new tab), when you type them on an empty line.
  • Control-D: ends the input, and psql quits.

\q is described in the psql documentation (opens in a new tab).

Step 1: Exit psql with \q

At the postgres=# prompt, type:

postgres=#Plain text
\q

psql closes, and you’re back at the Terminal prompt.

Step 2: Or Type exit or quit

postgres=#Plain text
exit

exit and quit work the same way, as long as they’re the only thing on the line. So does pressing Control-D.

Step 3: Exit When the Prompt Shows postgres-#

When the prompt ends in -#, psql is in the middle of a statement and waits for the rest of it. Typing exit there only reminds you of \q:

psql sessionPlain text
postgres=# SELECT 1
postgres-# exit
Use \q to quit.
postgres-# \q

Type \q: it quits even in the middle of a statement. To stay in psql and throw the half-typed statement away instead, press Control-C, and the prompt goes back to postgres=#.

Common Mistakes

MistakeWhat happensDo this instead
Typing exit while the prompt shows postgres-#Use \q to quit., and psql stays openType \q, or press Control-C first.
Typing \q after psql has already closedzsh: command not found: qNothing to do: you’re already back in Terminal.

FAQ

Why doesn’t Control-C exit psql?

Control-C cancels what you’re typing, or a query that’s still running, and keeps psql open. Use \q or Control-D to leave.

Does exiting psql stop the PostgreSQL server?

No. psql is only a client. The server keeps running in the background, ready for the next connection.

How do I leave a long list that ends with a colon?

That list is open in a pager, which shows long output one screenful at a time. Press q to go back to the postgres=# prompt, then \q to exit psql.

Conclusion

Type \q to exit psql. exit, quit and Control-D work too, on an empty line, and \q still works when the prompt shows postgres-#.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments