Is there an option that can change the "exit external editor -> query runs automatically" behaviour of the psql \e switch ? (Other than intentionally mangling the query syntax).
3 Answers
Don't add a semicolon (;
). I quote the manual about the \e
or \edit
meta-command here:
The new contents of the query buffer are then re-parsed according to the normal rules of psql, treating the whole buffer as a single line. Any complete queries are immediately executed; that is, if the query buffer contains or ends with a semicolon, everything up to that point is executed and removed from the query buffer. Whatever remains in the query buffer is redisplayed. Type semicolon or
\g
to send it, or\r
to cancel it by clearing the query buffer.
To be clear: only the last statement that is not terminated with a semicolon is not executed. To execute nothing at all, make sure there are no complete statements before the last statement. I.e. no semicolons at all.
That's really unsatisfying, though. See this follow-up:
.. which led Laurenz Albe to commit some improvements, live in psql 14 or later.
You can always prefix the query with the postgres comment string:
--select foo from bar;
and the query will not run but will be available in the psql deadline history buffer for editing.
If you have many request and some semicolons use Transaction :
In psql open editor mode \e
:
-- at start of edited text
BEGIN TRANSACTION;
-- ... your requests here
-- save exit
Back to psql : your requests are executed but change are not written :
If all is OK, save changes with :
COMMIT;
else If you have errors or want to cancel :
ROLLBACK;