psql Prompt Builder and .psqlrc PROMPT1 Preview
The psql prompt can show a lot more than the database name. With a few % escapes it can show the user and host you are connected to, whether you are inside a transaction (and whether that transaction has already failed), and, from psql 19, the current search_path and whether the server is a primary or a hot standby. Writing those escapes by hand is fiddly. Colors need %033 codes wrapped in %[ %] or readline miscounts the line width, and the \\set line in .psqlrc has its own quoting rules. Paste your current \\set PROMPT1 line or start from a preset. The tool shows the prompt in color for an idle session, an open transaction, a failed transaction and a continuation line, warns about escapes your psql version does not support, and gives you the lines to put in ~/.psqlrc. The renderer follows psql's own prompt.c and was checked byte by byte against psql 19 RC2 and 18.7. Everything runs in your browser.
\set PROMPT1 '%[%033[1;32m%]%n@%m%[%033[0m%]:%[%033[1;34m%]%/%[%033[0m%]%R%x%# ' \set PROMPT2 '%R%x%# '
Background reading: Customize the psql Prompt and .psqlrc.
Do more than psql prompt builder and .psqlrc prompt1 preview — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Pick a preset or paste the \set PROMPT1 and \set PROMPT2 lines from your ~/.psqlrc (just the quoted value works too).
- Insert escapes or colored segments, then set the psql and server versions, host and role to see how the prompt changes in each transaction state.
- Copy the generated \set lines into ~/.psqlrc (or %APPDATA%\postgresql\psqlrc.conf on Windows) and start a new psql session.
Frequently asked questions
How do I change the psql prompt permanently?
Put \set PROMPT1 '...' (and usually \set PROMPT2 '...') in ~/.psqlrc. psql runs that file at startup unless you start it with -X. You can keep different files per version, such as ~/.psqlrc-19, and psql reads the most specific one. The PSQLRC environment variable points psql at another file. To try a prompt first, run the same \set command in a session; it lasts until you quit.
Why does my colored psql prompt break line editing?
Color codes such as %033[1;32m are invisible, but readline cannot tell, so it counts them as characters. Then long commands wrap too early and recalled history lines are drawn over each other. Wrap every escape sequence in %[ and %], for example %[%033[1;32m%]%n%[%033[0m%], so readline knows that part takes no space. This tool warns when a sequence is not wrapped. psql's %w escape for PROMPT2 uses the same rule to work out the width of PROMPT1.
What do the new %S and %i prompt escapes in psql 19 do?
%S shows the current search_path and %i shows primary or standby, depending on whether the server is in hot standby. Both come from values the server reports to the client, so they update as soon as you run SET search_path or the server is promoted. In our tests against a PostgreSQL 17 server, psql 19 showed ? for %S, because servers before 18 do not report search_path. psql 18 and older do not know either escape and print a plain S or i. If you switch between many servers, Chat2DB (https://chat2db.ai/download or https://app.chat2db.ai) shows the connection, database and schema in its interface instead.
