Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: August 12, 2025
The SQL Shell, also known as psql, is the default command-line tool for interacting with PostgreSQL databases. It lets us run SQL queries, view tables, and manage data directly from the terminal.
The psql command-line utility provides several ways to exit the interface once we’re done working with it. We can use meta-commands, keyboard shortcuts, and even standard commands like exit or quit in newer versions. Each method enables us to close the session and return to the regular terminal or command prompt.
In this tutorial, we’ll review the different methods of exiting from the psql command-line utility.
psql supports various meta-commands, which are special commands starting with a backslash (\), to perform tasks such as listing tables, connecting to databases, and exiting the tool. We can quit the psql command-line interface by using the \q meta-command.
Let’s open the terminal and switch to the postgres user:
sudo -i -u postgres
After this, we use the psql command to access the SQL Shell as the postgres user:
psql
In psql, we can use SQL commands and meta-commands to perform different operations. For example, let’s use the \dt meta-command to list all tables in the current database:
\dt
Finally, we type \q at the psql prompt and press Enter. This immediately closes the session and returns us to our regular terminal:
Another way to exit psql is by using keyboard shortcuts. On Linux or macOS, we can press Ctrl+D, which sends an End-of-Transmission (EOT) signal to the terminal. This tells psql there is no more input, and it should close the session:
On Windows, the equivalent way to interrupt or exit is usually Ctrl+C, which stops the current process and quits psql. When we press Ctrl+C, it prompts a message to press Y or N. We can press Y and hit Enter to exit psql:
These shortcuts work only if we’re using psql from a terminal or command prompt, such as a Linux shell, macOS Terminal, or Windows Command Prompt. If we’re using psql from a graphical interface or an embedded terminal inside an IDE (like pgAdmin, DBeaver, or DataGrip), these shortcuts might not work as expected.
Newer versions of psql (starting from PostgreSQL 11) also let us use exit to close the session. This makes it more user-friendly, especially for those familiar with general command-line behavior. To use this command, all we need to do is type exit and press Enter:
Similarly, we can also use quit in the latest psql versions to exit the psql session and return to the regular terminal:
Both exit and quit are supported in modern versions of psql, but the official and most reliable method is still \q.
In this article, we explored various commands and keyboard shortcuts to exit the PostgreSQL command-line utility, psql.
Whether we use the traditional \q meta-command, keyboard shortcuts like Ctrl+D on Linux/macOS, Ctrl+C on Windows, or commands like exit and quit, each method enables us to close the session and return to the terminal with ease.
While all of these options work, \q remains the most consistent and reliable choice across different platforms.