1960

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
App Work
  • 21,899
  • 5
  • 25
  • 38
  • 8
    @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?](http://stackoverflow.com/q/468939/) – user272735 Jul 25 '12 at 16:04
  • 180
    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
  • 190
    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
  • 24
    @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
  • 50
    more importantly, this post is now the first hit when i google "exit psql" – Angel S. Moreno Sep 02 '14 at 04:42
  • 3
    Excellent example of horrible usability (of a otherwise great product)! This is why development is so frustrating sometimes... – Sliq Mar 09 '16 at 14:46
  • 2
    Thanks for this. Tried exit, quit, bye.. then \quit \exit. Eventually \h for help, but \? was what I should have used... – Mike Emery Jun 14 '16 at 14:24
  • 1
    @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. – Erwin Brandstetter Oct 30 '16 at 03:06
  • 1
    Other 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?). – Zbyszek Feb 14 '17 at 12:16
  • 2
    I 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. – NessBird Aug 07 '17 at 15:40
  • 1
    @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. – mbomb007 Apr 07 '20 at 17:08
  • 1
    spent 2 minutes to look for "exit" command in documentation. spent 10 seconds to google an answer on stackoverflow.com – java_newbie Apr 22 '20 at 10:59

9 Answers9

2675

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.

Frankline
  • 40,277
  • 8
  • 44
  • 75
  • 34
    This won't work if you are in single user backend mode (`--single`). Instead use [Kaarel's answer](http://stackoverflow.com/a/12106607/623735) (`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 07 '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
  • 2
    Type \? 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 01 '18 at 11:37
  • 1
    Ctrl-d also help – YOung Jul 26 '18 at 02:55
  • 1
    As of [PostgreSQL 11 you can now type "quit" or "exit"](https://www.postgresql.org/about/news/1894/). – aorth Oct 19 '18 at 08:23
  • 1
    @aorth Yes, they announced it a few months ago: https://stackoverflow.com/a/50513432/5070879 – Lukasz Szozda Oct 20 '18 at 10:11
  • `quit` may be deprecated and does not function. Just tried and error received `bash: quit: command not found`; `exit` cmd seems to work however – hnhl Jan 13 '22 at 10:46
  • @hnhl that are clearly commands for the bash shell, not postgresql shell. – Anders Jan 27 '23 at 00:39
808

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
Ted
  • 12,122
  • 4
  • 31
  • 39
320

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

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Kaarel
  • 3,958
  • 1
  • 18
  • 16
  • 7
    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
  • 9
    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
  • 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 07 '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
  • @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 :) – Jack May 06 '21 at 12:12
36

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
iusting
  • 7,850
  • 2
  • 22
  • 30
  • 7
    There is no need to "try" anything. The proper command to cleanly exit `psql` is [well documented](https://www.postgresql.org/docs/current/static/app-psql.html) and is `\q` –  Nov 07 '16 at 20:52
  • 3
    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 08 '16 at 07:29
  • 2
    Thank 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` nor `Ctrl+D` worked, but I could `Ctrl+Z` and then kill the suspended process – Sergey Apr 05 '18 at 23:02
  • 1
    You 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. – Gathide Jun 01 '21 at 03:41
26

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.

Community
  • 1
  • 1
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
10

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

sflitman
  • 111
  • 2
  • 5
10

Actually, \q, exit and CTRL + D didn't work for me to exit from the psql program.

Ctrl + Shift + D

This worked for me.

My Ubuntu version is 19.04.

halfer
  • 19,824
  • 17
  • 99
  • 186
VinoCoder
  • 1,133
  • 5
  • 22
  • 45
8

For Linux command line \q + enter.

Quiting with Ctrl + D also is works

Maurice Elagu
  • 764
  • 1
  • 10
  • 20
  • 6
    This just reiterates the answers from [Frankline](https://stackoverflow.com/a/9463370/3303195) and [Kaarel](https://stackoverflow.com/a/12106607/3303195). – faintsignal Nov 27 '18 at 16:01
1

You try type \q and then press ENTER to quit psql. Or type: quit

Viettel Solutions
  • 1,519
  • 11
  • 22