8

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).

8

Don't add a semicolon (;) and you are fine. 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.

0

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.

0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.