How to exit from PostgreSQL command line utility: psql – Postgresql

Photo of author
Written By M Ibrahim
postgresql postgresql-9.1 psql

Quick Fix: \q or quit or exit command can be used to exit the PostgreSQL command-line utility ‘psql’.

The Problem:

How do I exit the PostgreSQL command line interface psql utility and return to the system command prompt or shell?

The Solutions:

Solution 1: Type ‘\q’ and then press ‘ENTER’

To exit the PostgreSQL command line utility psql, type ‘\q’ and then press ‘ENTER’.

Note: As of PostgreSQL 11, you can also use the keywords "quit" or "exit" to leave the command-line tool.

Solution 2: Using the \q command

The most straightforward way to exit the PostgreSQL command-line utility, psql, is to use the \q command. This command will immediately exit psql and return you to the operating system’s command prompt. You can also use the quit() or quit commands to exit psql, but these commands may not work on all versions of psql.

Note: The backslash () character is required before the q command. If you omit the backslash, psql will interpret the q as a normal SQL command and will not exit.

Additional Tips:

  • You can also use the following keystrokes to exit psql:

    • Ctrl + D: This keystroke will send an end-of-file signal to psql, which will cause psql to exit.
    • Alt + Tab: This keystroke will switch you to a different window or tab, which will effectively exit psql.
  • If you are unsure of how to exit psql, you can always type help at the psql command prompt. This will display a list of all the available commands, including the \q command.

Solution 3: Exit PostgreSQL command line using Ctrl+D

To exit the PostgreSQL command line utility, you can use the following key combination:

Ctrl+D

This shortcut will immediately exit the psql console and return you to the command prompt.

Solution 4: Exit from PostgreSQL command line utility `psql`

Here are some ways to exit the PostgreSQL command line utility `psql`:

  • <kbd>Ctrl</kbd>+<kbd>Z</kbd> – This sends the TSTP signal, which stands for “terminal stop.”
  • <kbd>Ctrl</kbd>+<kbd>\\</kbd> – This sends the QUIT signal.

For Curiosity:

  • <kbd>Ctrl</kbd>+<kbd>D</kbd> – This sends the EOF character, which stands for “end of file.” However, this might not always work as expected, especially if any other character is entered before or if user input is not required at all.

Solution 5: Use quit, exit, or \q command

To exit the PostgreSQL command line utility psql, you can use the following commands:

quit: Enter the quit command to exit psql.

exit: Using the exit command will close the psql session and return you to the shell.

\q: This is another way to quit psql. It is a shortcut for the quit command.

These commands have been added to enhance the user experience. Previously, users often tried to quit psql using the quit or exit commands, but these were not recognized. The only way to quit psql was to use the \q command.

With the addition of the quit and exit commands, users can now quit psql in a more intuitive way. This makes the process of quitting PostgreSQL more enjoyable and user-friendly.