1946

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

12
  • 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?
    – user272735
    Jul 25, 2012 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, 2012 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, 2014 at 14:39
  • 24
    @Kheldar Indeed, it's just bad user interface design (coupled with arrogance). People are insecure about weird things. Mar 10, 2014 at 20:46
  • 50
    more importantly, this post is now the first hit when i google "exit psql" Sep 2, 2014 at 4:42

9 Answers 9

2671

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.

8
  • 33
    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, 2013 at 22:10
  • 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 1, 2018 at 11:37
  • 1
    Ctrl-d also help
    – YOung
    Jul 26, 2018 at 2:55
  • 1
    As of PostgreSQL 11 you can now type "quit" or "exit".
    – aorth
    Oct 19, 2018 at 8:23
  • 1
    @aorth Yes, they announced it a few months ago: stackoverflow.com/a/50513432/5070879 Oct 20, 2018 at 10:11
806

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
10
  • 4
    I tried ctrl-z, myself. It got the job done, more or less, but I wasn't entirely satisfied. :(
    – mjwach
    Jul 26, 2015 at 19:03
  • 17
    cntrl+D to exit from any where
    – vidur punj
    Jan 22, 2018 at 14:41
  • 4
    @mjwach ctrl+z just suspends the process to the background, almost certainly not what you want. Jul 6, 2018 at 5:31
  • 5
    Had a laugh with your answer. Brainsmashed due to all different tools we use.
    – Sergio A.
    Aug 21, 2019 at 12:24
  • 8
    I think you missed :q
    – Alan
    Apr 21, 2021 at 5:11
320

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

5
  • 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, 2015 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, 2015 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, 2017 at 15:43
  • 4
    @NessBird Ctrl is not the same as Cmd. Try Control-D instead of Command-D. Oct 31, 2017 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 6, 2021 at 12:12
35

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
4
  • 7
    There is no need to "try" anything. The proper command to cleanly exit psql is well documented and is \q Nov 7, 2016 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 8, 2016 at 7: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 5, 2018 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 1, 2021 at 3:41
25

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.

4
  • 2
    bad habits, bad habits everywhere
    – user4104817
    May 29, 2018 at 17:47
  • 1
    @randomware Could you elaborate? May 29, 2018 at 18:00
  • 1
    yes, 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
    – user4104817
    May 29, 2018 at 19:27
  • 6
    "There has been numerous recorded complaints" -> "backslash was satisfactory"? Heh. Jun 20, 2018 at 3:16
10

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

9

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.

0
8

For Linux command line \q + enter.

Quiting with Ctrl + D also is works

1
0

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

1
  • Worket for me on ubuntu 22 Feb 17 at 14:46

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