How to Exit From the Command Line Utility in PostgreSQL

Bilal Shahid Feb 02, 2024
  1. Use the \q Command to Terminate Script From the Command Line Utility in PostgreSQL
  2. Use exit to Exit From the Command Line Utility in PostgreSQL
  3. Press CTRL+C to Exit From the Command Line Utility in PostgreSQL
  4. Press Ctrl+Z to Pause From the Command Line Utility in PostgreSQL
  5. Press Ctrl+D to Terminate Shell Process in Bash, Linux, and Unix
  6. Press Ctrl+Z to Terminate Shell Process
  7. Press Ctrl+</kbd> to Quit From a PostgreSQL Session
  8. Use the \! Command to Terminate a Process in PostgreSQL
  9. Conclusion
How to Exit From the Command Line Utility in PostgreSQL

PSQL is a terminal-based front-end to the PostgreSQL database server. It uses files or command-line arguments from input and is portable and easy to use.

It also facilitates writing scripts and lets other tasks or sessions run correctly in the system’s background without the user worrying about the details.

Today, we will be learning how to exit from the PSQL command-line utility.

Use the \q Command to Terminate Script From the Command Line Utility in PostgreSQL

If you are not already there, log in to your PSQL from the command prompt using the following statement.

psql -u postgres

Enter the username and continue. To quit the open session, type any of the following.

\q

or

\quit

The PostgreSQL documentation defines these under the PSQL Options section. However, this will only terminate that specific script in a script file.

A script file is a .SQL file written for queries that run from inside the PSQL terminal. So if you tend to run that and call the \q command, it will terminate the running file and not our PSQL session.

Use exit to Exit From the Command Line Utility in PostgreSQL

PSQL is getting easier for programmers to run. If you type exit and run it in the command line, you will get the following:

Output:

Run the \q command or \quit to exit

In newer versions of PSQL, go ahead and type exit to quit your current PSQL session and return to the normal utility.

You can use this command to examine how to exit from the PSQL sessions.

\?

There is something known in PSQL as the SINGLE USER BACKEND MODE. The SINGLE USER has SUPER USER powers and primarily invokes this shell when there is a need for disaster recovery, and somehow the PostgreSQL server crashed or ran into faults.

In that scenario, you might not be able to run the \q command.

Press CTRL+C to Exit From the Command Line Utility in PostgreSQL

Another method we can use to exit from our current PSQL session is to press CTRL+C simultaneously.

CTRL+C is a shortcut for sending an INTERRUPT signal to the process running in the background from the shell initiated. It works by the terminal sending a SIGINT signal that tells the process to terminate quickly.

The PSQL utility will automatically issue the \q command and terminate the PostgreSQL session when you press these keys.

Press Ctrl+Z to Pause From the Command Line Utility in PostgreSQL

Another significant command that you can use to pause the process running is the combination of the Ctrl+Z keys and then pressing Enter.

This command will issue the SIGTSTP signal to the process, which will pause the process running in the background and return the user to the shell.

This command won’t terminate the process but rather pause it. Despite running to the shell, the process will still keep running and working in the system’s background once opened and initiated.

Press Ctrl+D to Terminate Shell Process in Bash, Linux, and Unix

Now, using this combination of keys won’t probably work in the PSQL shell. However, you can still try this for exiting the PostgreSQL shell in Bash, Linux, or Unix.

How does this work? The default value for the END OF FILE (EOF) is ^D, which is typed out by pressing Ctrl+D in the shell.

It is a special internal control character provided to the shell kernel by the device drivers for the terminal interface. So once you tell the shell that the input has to stop coming, it will terminate automatically.

Press Ctrl+Z to Terminate Shell Process

How does this command work? The Ctrl+Z sends a TSTP signal to the shell.

Signals are software interrupts and are used for communication between the user and the process. The TSTP signal is a short form for SIGTSTP, the SUSPEND signal for an operation.

It can register a signal handler for or ignore a signal. If the process is suspended, you can use the following command later to kill all processes once you return to the shell.

kill -l

Press Ctrl+</kbd> to Quit From a PostgreSQL Session

Once again, some users of PSQL may not be able to use this command, especially those on Windows. However, it may be effective for Linux or Bash users and other platforms.

This command works by sending a SIGQUIT signal, which means to QUIT, as seen by its name. It causes a process to get killed immediately and dumped to the core.

Core dumps are automatically produced on termination.

Use the \! Command to Terminate a Process in PostgreSQL

It is another handy command under the PSQL meta-commands documentation. To use this in your Postgres session, enter the following.

\!

Then press Enter to exit.

If you don’t provide an argument to this command, it will automatically return to the subshell. The rest of the line entered will be passed to the subshell where it will exit.

Conclusion

If any of the above Ctrl+Letter options does not work, try appending a Shift in the middle of these commands. You can try the following.

CTRL + SHIFT + D \ CTRL + SHIFT + \

We hope you learned in-depth about the different ways to exit from the PSQL shell. Always follow proper documentation to learn the correct ways to perform a procedure and follow our tutorials for better understanding in case of exceptions.

You can use this information to your benefit later and implement it the way you like.

Even though various commands perform similar functions, they have different operation methods. Whether the method suits you or your system the best is entirely dependent on you!

So, make sure you read through this thoroughly and implement the solutions as necessary.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

GitHub