How to exit from PostgreSQL command line utility: psql
Solution 1
Type \q
and then press ENTER
to quit psql
.
UPDATE: 19-OCT-2018
As of PostgreSQL 11, the keywords "quit
" and "exit
" in the PostgreSQL command-line interface have been included to help make it easier to leave the command-line tool.
Solution 2
My usual key sequence is:
quit()
quit
exit()
exit
q
q()
!q
^C
help
Alt + Tab
google.com
Quit PSQL
\q
I think veterans of the psql command line usually shorten that to just:
\q
Solution 3
Ctrl+D is what I usually use to exit psql console.
Solution 4
Use:
-
Ctrl+Z - this sends the
TSTP
signal (TSTP
is short for “terminal stop”) -
Ctrl+\ - this sends the
QUIT
signal
For curiosity:
-
Ctrl+D - this sends the
EOF
character.EOF
stands for "end of file". In this concrete case it exits from the psql subprogram, as the shell is waiting for user input. This should not be 'the way to go' as it is not working if: - any other character is entered before - try entering some white spaces and then press Ctrl+D, it's not going to exit psql.
- if the user input is not required at all
Solution 5
quit
or exit
or \q
Based on PostgreSQL 11 Beta 1 Released!:
User Experience Enhancements
Another feature that fell into this category was the inability to intuitively quit from the PostgreSQL command-line (psql). There has been numerous recorded complaints of users trying to quit with the quit and exit commands, only to learn that the command to do so was \q.
We have heard your frustrations and have now added the ability to quit the command-line using the keywords quit and exit and hope that quitting a PostgreSQL session is now as enjoyable as using PostgreSQL.
App Work
Updated on July 08, 2022Comments
-
App Work almost 2 years
What command or short key can I use to exit the PostgreSQL command line utility
psql
?-
user272735 almost 12 years@a_horse_with_no_name: I'm not shocked by the question, but the number of upvotes :) Compare e.g. to How do you quit the Vi editor with single keypress?
-
App Work over 11 yearsSometimes we need quick and straight forward answer than searching it in the manual to focus on the real problem.In such cases these short questions are really helpful.
-
Kheldar about 10 yearsThe real question is not "are people capable of reading a manual", but "should enterprise software respond to standard exit sequences" like, I don't know, "exit"? Having to read the manual to quit seems seriously counter-intuitive.
-
Iain Collins about 10 years@Kheldar Indeed, it's just bad user interface design (coupled with arrogance). People are insecure about weird things.
-
Angel S. Moreno over 9 yearsmore importantly, this post is now the first hit when i google "exit psql"
-
Sliq about 8 yearsExcellent example of horrible usability (of a otherwise great product)! This is why development is so frustrating sometimes...
-
Mike Emery almost 8 yearsThanks for this. Tried exit, quit, bye.. then \quit \exit. Eventually \h for help, but \? was what I should have used...
-
Erwin Brandstetter over 7 years@MartijnPieters: I think
\?
is the sensible answer to this lazy question and similar ones. Teach a man to fish etc ... And that's the one you deleted. -
Zbyszek about 7 yearsOther non-lazy answers for lazy questions: - "Can I have your number?" - "Yes, sure, look it up in a phone book." ;) I know, better to give a fishing rod than a fish. But I think it's not always the case - not when someone needs a fish very quickly or/and is a vegan and needs fish only once for uncle Tom that is visiting once a year. Best answer probably would be to give both. So after reading one would know how to quit psql and would know how to check how to quit (or look up other commands) if he/she miracelously doesn't know that already (didn't read info on the screen?).
-
NessBird over 6 yearsI usually quit psql in frustration because I can't figure out how to make it DO anything. The reason for this was because I didn't realize you must add a ; to the end of SQL statements in psql, or they are considered incomplete and don't get executed.
-
mbomb007 about 4 years@NessBird Pretty much any SQL shell requires semi-colons at the end of statements, even for MSSQL and other variants that don't normally require them in general.
-
java_newbie about 4 yearsspent 2 minutes to look for "exit" command in documentation. spent 10 seconds to google an answer on stackoverflow.com
-
-
hobs over 10 yearsThis won't work if you are in single user backend mode (
--single
). Instead use Kaarel's answer (Ctrl-D
). In addition to always working in pgsql it'll work in most your other unix shells (python, mysql, etc). If you always do things the "standard" way in 'nix your brain will be less cluttered with trivia. -
Ajedi32 almost 9 yearsYep. This also works in bash, sh, ssh, zsh, irb, pry, python, sudo su, node, and more. It is the standard way to exit a shell of any kind.
-
mjwach almost 9 yearsI tried ctrl-z, myself. It got the job done, more or less, but I wasn't entirely satisfied. :(
-
Kevin over 8 yearsNot just a shell. Any reasonably sane program which reads from stdin and interprets the empty string as EOF will accept ^D.
-
a_horse_with_no_name over 7 yearsThere is no need to "try" anything. The proper command to cleanly exit
psql
is well documented and is\q
-
iusting over 7 yearsAs @hobs clearly states about
\q
: "This won't work if you are in single user backend mode (--single). Instead use Kaarel's answer (CtrlD
)". IMHO usingCtrlD
is not the way to go either, and I explained why above and offered an alternative. -
NessBird over 6 yearsThis does not work for me, probably because I use the Dvorak keyboard layout on OSX. Neither cmd-D nor cmd-E (where D is on Qwerty) works.
-
Tilman Schmidt over 6 years@NessBird Ctrl is not the same as Cmd. Try Control-D instead of Command-D.
-
mkorpela over 6 yearsI actually typed that .. but it seems that on my machine it started to give me some output only after actually connecting to a database.
-
vidur punj over 6 yearscntrl+D to exit from any where
-
Jaywalker over 6 yearsType \? for help if just "help" doesn't help. This is a gleaming example of how not to create human computer interaction. Who thought of this great idea of \? for help and \q to quit?
-
Sergey about 6 yearsThank you!
Ctrl+Z
was the only command that worked for me - I was connected to a database via tunnel which lost the connection - neither\q
norCtrl+D
worked, but I couldCtrl+Z
and then kill the suspended process -
Admin almost 6 yearsbad habits, bad habits everywhere
-
Lukasz Szozda almost 6 years@randomware Could you elaborate?
-
Admin almost 6 yearsyes, i mean backslash was satisfactory and consistent with the other internal semicolonless pgsql commands, and one would do
\?
or\h
to continue learning 'everything else' imho -
Steve Bennett almost 6 years"There has been numerous recorded complaints" -> "backslash was satisfactory"? Heh.
-
LucidObscurity almost 6 years@mjwach ctrl+z just suspends the process to the background, almost certainly not what you want.
-
YOung almost 6 yearsCtrl-d also help
-
aorth over 5 years
-
Lukasz Szozda over 5 years@aorth Yes, they announced it a few months ago: stackoverflow.com/a/50513432/5070879
-
faintsignal over 5 years
-
Dzmitry Prakapenka over 5 yearspostgres=# /q postgres-# \q
-
Sergio A. over 4 yearsHad a laugh with your answer. Brainsmashed due to all different tools we use.
-
Rich Lysakowski PhD over 4 yearsYes, thank you for your humor. Mine was google.com then click the link for stackoverflow.com/questions/9463318/…
-
java_newbie about 4 yearsI wish all questions on SO could have one answer like this one.
-
Alan about 3 yearsI think you missed :q
-
Jack almost 3 years@Kevin you don't have to interpret the empty string as EOF.. by default sending ctrl+D will make you actually receive EOF from fgetc() (int=-1), and it will close the stdin file stream: any subsequent call to f*read*() will return error and feof(stdin) will return 1. So it's even easier to recognize it :)
-
Gathide almost 3 yearsYou may consider substituting the word 'try' with 'use' at the top of this answer. It brings in a feeling of 'not sure', yet this is a straight-forward and working answer to the question.
-
M. K. Hunter over 2 years/q definitely doesn't work.
-
hnhl over 2 years
quit
may be deprecated and does not function. Just tried and error receivedbash: quit: command not found
;exit
cmd seems to work however -
hundredrab about 2 yearsYou missed an 'Alt+Tab' after 'Quit PSQL' :P