How to Exit Postgres' Command Line Utility (SQL Shell)

Various command line utilities have different exit conventions, which makes it difficult to recall/remember the exact command needed to exit them. When it comes to Postgres, it provides a default command line interface, known as “SQL Shell” or “psql”. This interface enables users to perform database operations using different commands and meta-commands.

One of the most convenient ways of exiting the Postgres command line utility is by executing one of these special meta-commands. Moreover, if a user forgets the meta-command he can use the “\?” to get help regarding available meta-commands.

This post will illustrate the several methods to exit from Postgres’ command line utility.

How to Exit Postgres' Command Line Utility (SQL Shell)?

Use one of the below-exhibited methods to exit the Postgres command line utility:

- Using \q
- Using \quit
- Using CTRL + C
- Using CTRL + D (Linux)
- Using CTRL + Z (Windows)

Let’s start with “\q”.

How to Exit Postgres' Command Line Utility (SQL Shell) Using \q?

SQL Shell or psql supports a meta-command named “\q” that helps us quit the Postgres command line utility. Here is the practical demonstration of the stated meta-command:

\q

Executing the “\q” meta-command will ask you to press any key:

img

Pressing any keyboard key will close the SQL Shell.

How to Exit Postgres' Command Line Utility (SQL Shell) Using \quit?

Alternatively, users can execute the “\quit” meta-command to exit/quit the Postgres command line utility. Here is the practical implementation of the stated meta-command:

\quit

Press any keyboard key to exit the SQL Shell:

img

How to Exit Postgres' Command Line Utility (SQL Shell) Using CTRL + C?

The “CTRL + C” is the shortcut key for exiting the Postgres command line utility. For a better understanding of the given shortcut key, check out the below-given snippet:

CTRL + C

Press Y and hit ENTER to terminate the Postgres’ command line utility:

img

How to Exit Postgres' Command Line Utility (SQL Shell) Using CTRL + D?

In the Linux operating system, the CTRL + D is a shortcut key that allows us to exit from the Postgres command line utility. Check out the below-given steps for a profound understanding of the stated shortcut key:

Step 1: Access Postgres From Terminal

Launch the terminal, and use the following “sudo” command to access the Postgres command line utility:

sudo -u postgres psql
img

Step 2: Access Postgres From Terminal

Now hit the “CTRL + D” shortcut key on the keyboard to exit the Postgres command line utility:

CTRL + D

The output snippet shows pressing the CTRL + D shortcut key exited us from the Postgres command line utility:

img

How to Exit Postgres' Command Line Utility (SQL Shell) Using CTRL + Z?

In the Windows operating system, the CTRL + Z is a shortcut key that helps us exit from the Postgres command line utility. Go through the below-provided steps to find out how this shortcut key works:

Step 1: Access Postgres From CMD

Launch the CMD, and use the cd command to get into the Postgres bin directory:

cd \Program Files\PostgreSQL\15\bin
img

Step 2: Connect to Postgres

Now use the below-stated psql command to connect to the Postgres:

psql -U postgres
img

Step 3: Exit Postgres Command Line Utility

Now type in the “CTRL + Z” shortcut key on the keyboard and hit the ENTER button to exit the Postgres command line utility:

CTRL + Z

The output snippet shows that we have successfully exited the Postgres command line utility:

img

That’s all about exiting the Command Line Utility (SQL Shell).

Conclusion

In PostgreSQL, various methods are used to exit the Postgres command line utility, such as the “\q” or “\quit” commands, and “CTRL + C”, “CTRL + D”, or “CTRL + Z” shortcut keys. The most convenient way of exiting the Postgres command line utility is to execute the “\q” or “\quit” meta-commands. This post has illustrated several methods for exiting the Postgres command line utility, i.e., SQL Shell or psql.