Exit from the command line utility in PostgreSQL
PSQL is a terminal-based front-end for the PostgreSQL database server. It uses either command-line arguments from an input file or from the command line, and is portable and easy to use.
It also helps in writing scripts and letting other tasks or sessions run correctly in the background of the system without the user having to worry about the details.
Today, we will learn how to exit the PSQL command line utility.
\q
Terminate the script from the PostgreSQL command line utility using
If you haven't already, log into your PSQL from the command prompt using the following statement.
psql -u postgres
Enter your username and continue. To exit an open session, type any of the following.
\q
or
\quit
The PostgreSQL documentation Options
defines these under the PSQL section. However, this will only terminate a specific script in the script file.
Script files are files written for queries to be run from within the PSQL terminal .SQL
. So if you tend to run it and call \q
the command, it will terminate the running file instead of our PSQL session.
exit
To exit from the PostgreSQL command line utility, use
PSQL is getting easier and easier for programmers to run. If you type exit
and run it at 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 exit the current PSQL session and return to the normal utility.
You can check how to exit a PSQL session using this command.
\?
There is something called PSQL in 单用户后端模式
. SINGLE USER
has SUPER USER
the privilege, and this shell is mainly called when disaster recovery is needed, and the PostgreSQL server has crashed or encountered a failure in some way.
In this case, you may not be able to run \q
the command.
Press CTRL+ Cto exit the PostgreSQL command line utility
Another method we can use to exit the current PSQL session is to press CTRL+ at the same time C.
CTRL+ Cis a shortcut for sending INTERRUPT
a signal to a process running in the background from a launched shell. It sends a signal through the terminal SIGINT
to tell the process to terminate quickly.
When you press these keys, the PSQL utility will automatically issue \q
the command and terminate the PostgreSQL session.
Press Ctrl+ Zto pause from the PostgreSQL command line utility
Another important command you can use to pause a process is the combination of the Ctrl+ Zkey and then press Enter
.
This command will send SIGTSTP
a signal to the process, which will pause the process running in the background and return the user to the shell.
This command does not terminate the process, but rather suspends it. Although running to a shell, once opened and started, the process will continue to run and work in the background on the system.
CTRL+D
Terminate the Shell Process in Bash, Linux and Unix by pressing
Now, using this key combination may not work in the PSQL shell. However, you can still try to exit the PostgreSQL shell in Bash, Linux, or Unix.
How does this work? The default value for END OF FILE (EOF) is ^D
, which is entered by pressing Enter in the shell CTRL+D
.
It is a special internal control character provided to the shell kernel by the terminal interface device driver, so once you tell the shell that input must stop, it will terminate automatically.
Press Ctrl+ Zto terminate the Shell process
How does this command work? Ctrl+ ZSends TSTP
a signal to the shell.
A signal is a software interrupt used for communication between a user and a process. TSTP
Signal is SIGTSTP
the abbreviation for an operation SUSPEND
.
It can register a signal handler or ignore a signal. If the processes are hung, you can kill all processes later after returning to the shell using the following command.
kill -l
Press Ctrl+</kbd> 退出 PostgreSQL 会话
Again, some PSQL users may not be able to use this command, especially those on Windows. However, it may work for Linux or Bash users and other platforms.
This command works by sending a SIGQUIT
signal, which means quit, as you can tell from its name. It causes the process to be killed immediately and dumped to core.
Core dumps are automatically generated on termination.
\!
Use the command to terminate the process in PostgreSQL
This is another handy command under the PSQL meta-command documentation. To use it in your Postgres session, enter the following.
\!
Then press to Enter
exit.
If you do not provide arguments to this command, it will automatically return to the subshell. The remainder of the line entered will be passed to the subshell which will exit.
in conclusion
If any of the above Ctrl+ Letteroptions don't work, try appending one in the middle of these commands Shift
. You can try the following.
CTRL + SHIFT + D \ CTRL + SHIFT + \
We hope you have gained a good understanding of the different ways to exit the PSQL shell. Always follow the appropriate documentation to understand the correct way to execute a program and follow our tutorials for better understanding in case of exceptions.
You can use this information to your advantage later and implement it in the way you like.
Although the various commands perform similar functions, they have different methods of operating them. Whether or not that method works best for you or your system is entirely up to you!
So make sure you read this thoroughly and implement the solutions as needed.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab