3.7 Useful Shortcuts: \e, \g, \watch, and \timing
Right, let’s talk about the four shortcuts that will save you more cumulative hours than any other feature in psql. These aren’t just commands; they’re workflow accelerants. You’ll wonder how you ever lived without them.
The Magical Do-Over: \e
You’ve just written a beautiful, complex, four-line query. You hit enter. A typo. Or you realize you need to add a WHERE clause. Your heart sinks. Do you press the up arrow and try to edit this behemoth in the terminal, hoping you don’t accidentally delete a parenthesis and summon a demon? No. You type \e.
This command opens your last query in the text editor defined by your $EDITOR environment variable (usually vim or nano). Fix your typo, add your JOIN, write a novel—do whatever you need. Save and close the file. psql will immediately execute the revised query. It’s the ultimate do-over.
But here’s the pro move: you can also use \e on its own to open a new buffer. Write a fresh query from scratch in the comfort of a real editor, save, and execute. It’s far superior to trying to craft a masterpiece line-by-line in the terminal. If you already have a query in the buffer, you can combine these powers: SELECT * FROM \e will open your editor with SELECT * FROM already there, ready for you to complete.
-- You're in psql, you type this and mess it up:
SELECT user_id, email, last_login FROM users WHERE is_active = true ORDER BY last_login DESC;
-- Instead of arrowing up, you just type:
\e
-- Fix it in your editor, save, and it runs the corrected version.
The Re-execute and Re-purpose Command: \g
You probably think you execute a query by ending it with a semicolon. And you’re right. But the semicolon is just a terminator for the query buffer. The actual command to send that buffer to the server is \g. The semicolon is just the default signal to do that.
Why should you care? Because \g is far more powerful. It lets you re-execute the last query buffer, even if you’ve moved on and typed other things. It’s like a dedicated “run again” button.
But its real power is in re-purposing. You can use \g to send the current query buffer to a different output. The most common use is \gx, which executes the query but forces expanded mode (like \x on), which is glorious for wide tables with JSON columns.
-- You run a query that returns wide results, and it's a mess.
SELECT * FROM user_properties LIMIT 1;
-- You realize you need expanded output to read it.
-- Instead of rewriting the whole thing, just:
\gx
-- The same query runs again, but now in the clean, vertical, expanded format.
The Lazy Person’s Dashboard: \watch
This command is brilliantly simple and endlessly useful. You prefix any query with \watch [interval], and psql will execute it over and over again, on a loop, at the specified interval (default is 2 seconds).
Need to monitor a queue length? Watch a long-running batch process? See if your application’s users are actually doing the thing you just deployed? This is your tool. It turns psql into a real-time monitoring dashboard. It’s watch for SQL. Hit Ctrl+C to break the cycle when you’re done.
The designers, for once, nailed it. It’s perfect.
-- See how many tasks are pending in your job queue, updated every 5 seconds
SELECT count(*) FROM job_queue WHERE status = 'pending'\watch 5
You’ll get a live-updating output right in your terminal. It’s absurdly useful for debugging and monitoring.
The Humility Generator: \timing
Type \timing. Just do it. It toggles a mode where psql will report the execution time of every query you run, down to the millisecond. This isn’t the planner’s estimate; this is cold, hard reality.
Why is this a humility generator? Because you’ll write a query you think is clever and optimized, only to see Time: 1250.345 ms (00:01.250) stare back at you. It’s the fastest way to realize your WHERE clause is missing an index or your 8-way JOIN is a disaster. It’s also incredibly satisfying when you fix it and see Time: 2.101 ms.
Toggle it on, leave it on. The overhead is negligible, and the insight is invaluable. It’s the single best tool for performance awareness right at your fingertips.
-- Toggle timing on
\timing
-- Now run any query
SELECT * FROM generate_series(1,1000000);
-- You'll see something like:
Time: 125.234 ms
The Pitfall: The only real “gotcha” with these is that they operate on the query buffer. If you use \e and then, before executing, type another command, you’ve overwritten your beautifully crafted query. The buffer is a fickle thing. The best practice is to use these commands deliberately. Use \e for composition and major edits, and use \g, \watch, and \timing for execution control and introspection. Master these four, and you’ve just leveled up from psql user to psql wizard.