Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

What command or short key can I use to exit the PostgreSQL command line utility psql?

share|improve this question
2  
@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
134  
Sometimes 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
116  
The 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
15  
@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
34  
more importantly, this post is now the first hit when i google "exit psql" – Angel S. Moreno Sep 2 '14 at 4:42
up vote 1507 down vote accepted

Type \q and then press ENTER to quit psql.

share|improve this answer
11  
simple but useful. Thanks!! – Hoàng Long May 30 '12 at 8:00
188  
I can't believe I had to google to work that out :-) Thanks. – Dave Sag Jun 25 '13 at 1:29
9  
This 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
1  
yes, very simple solution. I guessed \quite, \exit but none of them worked, finally got solution after googeling. Thanks :) – Ravi Dhoriya ツ May 15 '14 at 10:28
6  
I was going to upvote @DaveSag - but then I thought, why can we not use "quit" or "exit" like a normal sql program? – nycynik Jun 19 '14 at 21:53

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
share|improve this answer
2  
That's usually what I do too – Jehan Jul 18 '14 at 22:44
12  
ROFL!!!!, @ted you must be a system admin. – Angel S. Moreno Sep 2 '14 at 4:44
39  
thats pretty much how I got here, glad to know I'm doing it right. – brent.payne Nov 4 '14 at 17:37
4  
This just shows how arrogant the PostgreSQL developers have been. Really, \q? What's wrong with exit or Ctrl+C? – Dan Dascalescu Sep 23 '15 at 1:36
5  
I've been back here about seven or eight times since I posted this. – Ted Oct 25 '16 at 14:57

Ctrl+D is what I usually use to exit psql console.

share|improve this answer
1  
Thanks, very useful short key. – App Work Aug 26 '12 at 7:14
28  
This works on almost all prompts, people! – dbkaplun Jun 28 '13 at 17:27
    
Yep. 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
2  
Not 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

For Linux command line \q + enter.

Quiting with Ctrl + D also is works

share|improve this answer

I learned that I could include \q in a batch .sql file, so I could have psql quit earlier from an \i operation.

share|improve this answer

Try:

  • 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
share|improve this answer
    
There is no need to "try" anything. The proper command to cleanly exit psql is well documented and is \q – a_horse_with_no_name Nov 7 '16 at 20:52
    
As @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 using CtrlD is not the way to go either, and I explained why above and offered an alternative. – iusting Nov 8 '16 at 7:29

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?

Not the answer you're looking for? Browse other questions tagged or ask your own question.