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
\q
\q: quits psql. It’s one of psql’s own backslash commands, so it needs no semicolon.exitorquit: 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:
\q
psql closes, and you’re back at the Terminal prompt.
Step 2: Or Type exit or quit
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:
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
| Mistake | What happens | Do this instead |
|---|---|---|
Typing exit while the prompt shows postgres-# | Use \q to quit., and psql stays open | Type \q, or press Control-C first. |
Typing \q after psql has already closed | zsh: command not found: q | Nothing 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
- PostgreSQL 19 documentation: psql (opens in a new tab)
- PostgreSQL 11 release notes (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 2: Connect with psql and Run Your First Commands
- How to Check Your PostgreSQL Version
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