If your PostgreSQL SQL Shell window is crowded with old commands and results, you can clear the screen without closing your database session or deleting any data. The quickest method depends on your operating system: use \! cls in Windows, \! clear on macOS or Linux, or try the terminal’s clear-screen keyboard shortcut. If you only want to erase a command you have started typing, use a different psql command. This guide explains how to clear SQL Shell, reset its query buffer, troubleshoot common issues, and avoid confusing a clean screen with clearing your database.
What Does “Clear SQL Shell” Mean?
“SQL Shell” commonly refers to psql, PostgreSQL’s interactive command-line client. It lets you connect to a PostgreSQL server, enter SQL statements, run them, and view the results in a terminal window. As you work, commands and output accumulate on the screen, which can make it harder to focus on your current task.
Clearing the screen removes the visible text from the terminal display. It does not disconnect you from PostgreSQL, erase your command history, undo a query, or remove information from a database. Your existing connection normally remains active, so you can continue working immediately.
There is a second meaning that can cause confusion: clearing the current, unfinished SQL command. If you have typed a statement but have not run it, the psql command \r clears the query buffer. That is different from clearing the terminal screen. The right option depends on what you want to remove.
How to Clear SQL Shell on Windows
In the Windows SQL Shell (psql) window, enter this command at the psql prompt and press Enter:
\! cls
The \! part tells psql to run a command in the operating system’s command shell. The word cls is the Windows command for clearing the console screen. Once it runs, the old visible output should disappear and the psql prompt should return.
Make sure you enter the command at a psql prompt, such as postgres=# or mydb=>, rather than in the middle of an unfinished SQL statement. Psql commands begin with a backslash and do not need a semicolon. If you accidentally type \! cls;, the semicolon may be passed as part of the command and could cause an error.
You can also try pressing Ctrl+L. This shortcut clears the screen in many terminal environments, but its behavior depends on the console, terminal emulator, and input configuration. If it does not work in your SQL Shell window, use \! cls instead.
How to Clear SQL Shell on macOS or Linux
On macOS and Linux, use the operating system’s clear command through psql. At the psql prompt, enter:
\! clear
Press Enter to run it. The command clears the terminal display while leaving your PostgreSQL session connected. As on Windows, do not add a semicolon to the psql command.
Depending on your terminal application, Ctrl+L may offer a quicker alternative. It commonly clears or redraws a Unix-style terminal, but the exact result can vary. Some terminals clear the visible text while keeping it available in scrollback. If the screen still looks cluttered, or the shortcut does not work, run \! clear.
If your shell does not recognize clear, the command-line utility may be missing or unavailable in your environment. Try Ctrl+L, use another terminal application, or consult your system administrator. The psql command itself is still available; it relies on the operating system to perform the screen-clearing action.
Clear the Current SQL Command with \r
Use \r when you want to discard an unfinished statement rather than clear everything displayed in SQL Shell. For example, you may have started typing a long query and realize that you want to start over. Enter:
\r
Psql resets the current query buffer. This removes the SQL text that has been collected for the statement but does not clear previous results from the terminal. It also does not affect statements that have already been executed.
This distinction matters because psql can accept a query across multiple lines. If you type SELECT * and press Enter without finishing the statement, the prompt may change to show that psql is waiting for more input. You can finish the query with a semicolon, or discard it with \r. To clear the screen as well, run the appropriate operating-system command afterward.
Quick Reference: Which Command Should You Use?
-
Clear the visible SQL Shell screen on Windows:
\! cls -
Clear the visible SQL Shell screen on macOS or Linux:
\! clear -
Try a keyboard shortcut: Press
Ctrl+Lif your terminal supports it. -
Discard an unfinished SQL statement: Enter
\r. -
Leave psql: Enter
\q.
These commands do different jobs. In particular, \q exits psql; it does not simply tidy the screen. Use it only when you intend to close your interactive session.
Does Clearing SQL Shell Delete Database Data?
No. The screen-clearing commands \! cls and \! clear only affect what you see in the terminal. They do not delete tables, rows, databases, query results stored elsewhere, or PostgreSQL logs. Likewise, \r discards an unfinished command from the psql buffer; it does not undo a query that has already run.
To change or remove database data, you would need to run SQL statements such as DELETE, TRUNCATE, or DROP. Those commands can have significant consequences, so do not use them as a way to clear the SQL Shell display. If your goal is simply to get a fresh-looking prompt, use the screen-clearing command for your operating system instead.
Troubleshooting SQL Shell Screen-Clearing Commands
If the command does not work as expected, check these common causes:
-
You used the wrong operating-system command. Use
clson Windows andclearon macOS or Linux. Each command is intended for its respective environment. -
You typed it as SQL. The backslash in
\! clsor\! clearmarks a psql meta-command. Enter it directly at the psql prompt, not inside a SQL statement. - You added a semicolon. Psql meta-commands generally end when you press Enter and do not require a semicolon. Try the command without one.
-
You are still inside an unfinished statement. If psql is waiting for the rest of a query, use
\rto clear the query buffer, then enter the screen-clearing command at the normal prompt. - Your terminal preserves scrollback. Clearing the visible screen may not erase its scrollback history. A user may still be able to scroll upward and see earlier output, depending on the terminal.
-
The shortcut is unsupported.
Ctrl+Lvaries between terminal applications. Use the operating-system command instead.
If your screen clears but you do not see the usual psql prompt, wait briefly or press Enter once. If you are unsure whether psql is connected, look for the prompt or run a harmless psql meta-command such as \conninfo to display connection information. Avoid entering database-changing SQL merely to test whether the interface is responding.
Keep Your SQL Shell Easier to Read
Clearing the display is useful when you have run several queries, received lengthy output, or want to capture a clean screenshot. You can make the shell easier to work in by running one query at a time, using a clear terminal window size, and choosing concise output where appropriate. For large result sets, consider adding a LIMIT clause while exploring data so that the terminal is not filled with more rows than you need.
Remember that clearing the screen is temporary: it changes the current display, not the history or contents of your database. Your terminal may retain scrollback, and psql may maintain command history depending on its configuration. Do not use a screen clear as a privacy or security measure when sensitive output has been displayed. Follow your organization’s guidance for handling confidential information.
Conclusion
To clear PostgreSQL SQL Shell, run \! cls on Windows or \! clear on macOS and Linux. You can also try Ctrl+L if your terminal supports it. If you meant to erase an unfinished SQL statement instead, use \r. These actions clear the display or query buffer—not your database—and they do not close your connection. Choosing the command that matches your goal makes it easy to get a clean SQL Shell screen and continue working safely.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.