What command or short key can I use to exit the PostgreSQL command line utility psql?
-
4@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? – user272735 Jul 25 '12 at 16:04
-
173Sometimes 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. – App Work Nov 16 '12 at 11:35
-
173The 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. – Kheldar Feb 20 '14 at 14:39
-
23@Kheldar Indeed, it's just bad user interface design (coupled with arrogance). People are insecure about weird things. – Iain Collins Mar 10 '14 at 20:46
-
49more importantly, this post is now the first hit when i google "exit psql" – Angel S. Moreno Sep 2 '14 at 4:42
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.
-
25This 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. – hobs Nov 7 '13 at 22:10 -
I actually typed that .. but it seems that on my machine it started to give me some output only after actually connecting to a database. – mkorpela Dec 30 '17 at 10:48
-
1Type \? 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? – Jaywalker Feb 1 '18 at 11:37
-
1
-
1@aorth Yes, they announced it a few months ago: stackoverflow.com/a/50513432/5070879 – Lukasz Szozda Oct 20 '18 at 10:11
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
-
3I tried ctrl-z, myself. It got the job done, more or less, but I wasn't entirely satisfied. :( – mjwach Jul 26 '15 at 19:03
-
10
-
@mjwach ctrl+z just suspends the process to the background, almost certainly not what you want. – LucidObscurity Jul 6 '18 at 5:31
-
-
Ctrl+D is what I usually use to exit psql console.
-
57
-
2Yep. 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. – Ajedi32 Jul 10 '15 at 19:46
-
4Not just a shell. Any reasonably sane program which reads from stdin and interprets the empty string as EOF will accept ^D. – Kevin Aug 20 '15 at 20:37
-
This 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. – NessBird Aug 7 '17 at 15:43
-
4@NessBird Ctrl is not the same as Cmd. Try Control-D instead of Command-D. – Tilman Schmidt Oct 31 '17 at 14:06
Try:
- Ctrl+Z - this sends the
TSTPsignal (TSTPis short for “terminal stop”) - Ctrl+\ - this sends the
QUITsignal
For curiosity:
- Ctrl+D - this sends the
EOFcharacter.EOFstands 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
-
4There is no need to "try" anything. The proper command to cleanly exit
psqlis well documented and is\q– a_horse_with_no_name Nov 7 '16 at 20:52 -
2As @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 usingCtrlDis not the way to go either, and I explained why above and offered an alternative. – iusting Nov 8 '16 at 7:29 -
1Thank you!
Ctrl+Zwas the only command that worked for me - I was connected to a database via tunnel which lost the connection - neither\qnorCtrl+Dworked, but I couldCtrl+Zand then kill the suspended process – Sergey Apr 5 '18 at 23:02
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.
-
2bad habits, bad habits everywhere – user4104817 May 29 '18 at 17:47
-
1
-
1yes, i mean backslash was satisfactory and consistent with the other internal semicolonless pgsql commands, and one would do
\?or\hto continue learning 'everything else' imho – user4104817 May 29 '18 at 19:27 -
5"There has been numerous recorded complaints" -> "backslash was satisfactory"? Heh. – Steve Bennett Jun 20 '18 at 3:16
For Linux command line \q + enter.
Quiting with Ctrl + D also is works
-
3
I learned that I could include \q in a batch .sql file, so I could have psql quit earlier from an \i operation.
This is POSTGRESQL - running on CentOS7 , kindly note i have exited from PSQL with \q , then i am still within the bash Shell for PostgreSQL , which i again Logout with - logout .... terminal output below ---- -bash-4.2$ quit -bash: quit: command not found -bash-4.2$ exit logout There are stopped jobs. -bash-4.2$ logout
protected by Erwin Brandstetter Sep 18 '14 at 22:48
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?