If your PostgreSQL screen is crowded with query results, prompts, or error messages, you may be wondering how to clear the psql terminal. The quickest options are usually to press Ctrl+L or run a terminal command from inside psql. The right method depends on your operating system and whether you want to clear the visible screen, erase an unfinished query, or interrupt a command that is still running.
This guide explains how to clear the psql screen on Linux, macOS, and Windows, what to do when the usual shortcuts do not work, and how clearing the display differs from resetting psql’s query buffer. These steps help keep your PostgreSQL session readable without accidentally changing your database or losing your place.
What Does “Clear the Psql Terminal” Mean?
People use “clear psql” to describe a few different actions. Most often, they mean removing the text currently displayed in the terminal window. This makes the screen easier to read, but it does not delete previous commands from the terminal’s scrollback or psql’s history file. It also does not undo SQL statements that have already run.
Other times, a user wants to discard a partially typed SQL statement, stop a long-running query, or return to a clean prompt. Those actions require different commands. Knowing which one you need can help prevent mistakes, especially when working in a production database.
- Clear the visible screen: Remove the text currently shown in the terminal.
- Clear an unfinished SQL statement: Empty psql’s current query buffer.
- Stop an active query: Send an interrupt to the running command.
- Clear terminal scrollback or history: Remove content that may still be accessible by scrolling or through saved history.
The Fastest Way: Press Ctrl+L
In many terminal applications, you can clear the visible psql screen by pressing Ctrl+L. Hold down the Ctrl key and press L once. If the terminal supports this shortcut, the current display will clear and the psql prompt will remain ready for your next command.
This shortcut is commonly available in Linux terminals, macOS terminal apps, and terminal emulators used with SSH. It may also work in Windows Terminal or other Windows terminal applications, depending on their settings. Because Ctrl+L is handled by the terminal or its line-editing library, it is not a SQL command and does not send a query to PostgreSQL.
If nothing happens, your terminal may not map Ctrl+L to a clear-screen action. Try the operating-system command described below, or use the terminal application’s own clear-screen menu option. Clearing the screen does not end your psql session, close the connection, or change database data.
Clear Psql on Linux or macOS
On Linux and macOS, the standard terminal command for clearing the display is clear. Since you are inside psql, you need to ask psql to run that command in the system shell. Enter the following at the psql prompt:
\! clear
The command begins with \!, a psql escape that runs a command through the operating system shell. The word clear is the shell command that refreshes the terminal display. After it runs, you should return to the familiar psql prompt, such as => or =#.
You can also leave psql temporarily, run clear in the shell, and start psql again, but that is usually unnecessary. Using \! clear keeps your current database session open. If your terminal does not recognize clear, check that the command is available in your shell and that the terminal environment is configured correctly.
Clear Psql on Windows
On Windows, the screen-clearing command is typically cls. From the psql prompt, try:
\! cls
Here, psql passes cls to the Windows command environment. This works in many standard Windows setups. If it does not work in your terminal, try pressing Ctrl+L, using the terminal’s clear-screen option, or running psql from a shell where the appropriate clear command is available.
Windows users may run psql through Command Prompt, PowerShell, Windows Terminal, a database management application, or an SSH client. These environments can handle keyboard shortcuts and shell commands differently. If \! cls reports an error, the issue may be the shell or terminal configuration rather than PostgreSQL itself.
Use the Psql Shell Escape Correctly
The \! syntax is a psql meta-command, not SQL. Meta-commands are instructions interpreted by the psql client. They begin with a backslash and generally do not need a semicolon. SQL statements, by contrast, are sent to the PostgreSQL server and usually end with a semicolon.
For example, type \! clear or \! cls directly at the psql prompt. Do not type either one as part of a SQL statement or add it after a SQL semicolon on the same line. If psql is waiting for the end of an unfinished statement, first return to a normal prompt or cancel the pending input.
Running a shell command from psql can be useful, but use it thoughtfully. The command after \! is executed by your local operating system, not by the PostgreSQL server. It therefore operates in the environment where the psql client is running, which may be your computer or a remote machine if you connected to a server through SSH.
Clear an Unfinished Query with \r
If you have started typing SQL but do not want to run it, clearing the screen will not remove the unfinished statement. Psql keeps the text in a query buffer until you execute it or discard it. To empty that buffer, enter:
\r
This psql meta-command resets the current query buffer. It is especially helpful if you accidentally left out a closing quote, parenthesis, or semicolon and the prompt changed to indicate that psql is waiting for more input. After using \r, you can begin a new statement at a fresh prompt.
Look at the prompt before entering a command. A regular prompt often ends with => or =#, while continuation prompts may look like ->, '>, or another character that signals psql is still collecting input. If you are unsure whether your SQL is complete, use \r to discard the buffer rather than accidentally executing an unintended command.
Stop a Running Query or Command
Clearing the display does not stop a query that is currently running. To interrupt a query in psql, press Ctrl+C. Depending on the situation, this may cancel the active query or cancel the text you are entering and return you to a usable prompt.
Use Ctrl+C when a query is taking too long or you realize you started the wrong command. Do not confuse it with Ctrl+L: Ctrl+C is for interruption, while Ctrl+L is commonly used to redraw or clear the screen. If a query has already completed and changed data, interrupting afterward will not reverse those changes. Use a transaction and an appropriate rollback when you need to undo work that is still uncommitted.
If psql is displaying query output through a pager such as less, the screen may not respond to psql commands because the pager currently has control. Press q to exit the pager and return to the psql prompt. You can then clear the terminal with Ctrl+L, \! clear, or \! cls, as appropriate.
Does Clearing Psql Delete Command History?
No. Clearing the visible terminal usually does not delete your psql command history. It simply moves or refreshes what is displayed. You may still be able to scroll upward and see earlier output, depending on your terminal, and psql may retain commands in its history for the session or save them to a history file.
There are also two different kinds of history to consider: the terminal’s scrollback buffer and psql’s command history. Clearing the display may leave scrollback untouched. Similarly, removing text from the screen does not necessarily remove commands that can be recalled with the keyboard’s history keys.
If you need to remove sensitive output from view, use your terminal application’s option to clear scrollback, if it provides one. Be aware that this may not erase psql’s saved command history or security logs. Avoid typing passwords or other secrets directly into SQL commands, since they could be recorded in history or exposed in other ways.
Common Problems and Quick Fixes
-
Ctrl+Ldoes nothing: Try\! clearon Linux or macOS, or\! clson Windows. Check whether your terminal assigns a different shortcut. -
\! clearor\! clsis not recognized: Confirm that you are using the command for your operating system and that psql is running in a compatible shell. -
You see a continuation prompt: Psql may be waiting for more SQL. Enter
\rto discard the unfinished query, or complete the statement if you intend to run it. -
The query is still running: Press
Ctrl+Cto try to cancel it. Clearing the screen alone does not interrupt database work. -
You cannot type a psql command: You may be viewing output in a pager. Press
qto return to the psql prompt. - Old text appears when you scroll: The terminal’s scrollback was not cleared. Use the terminal application’s clear-scrollback feature if available.
Quick Reference: Psql Clear Commands
-
Clear or redraw the screen: Press
Ctrl+Lin supported terminals. -
Clear the screen on Linux or macOS: Run
\! clearinside psql. -
Clear the screen on Windows: Run
\! clsinside psql. -
Discard an unfinished SQL statement: Enter
\r. -
Interrupt a running query or current input: Press
Ctrl+C. -
Exit a pager: Press
q. -
Quit psql: Enter
\q.
Remember that \q closes the psql client; it does not just clear the screen. Use it only when you intend to leave your session. Likewise, clearing the terminal is a display action, not a way to reset your database or undo SQL.
Conclusion
To clear the psql terminal, start with Ctrl+L if your terminal supports it. Otherwise, use \! clear on Linux or macOS, or \! cls on Windows. These options clear the visible display while keeping your psql session open.
If you actually need to discard unfinished SQL, use \r. To interrupt a running query, press Ctrl+C, and to leave a pager, press q. Choosing the command that matches your goal keeps your terminal tidy without confusing screen cleanup with query cancellation, history deletion, or database changes.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.