Does anyone know if Postgres has a way to display query results "prettily", like how MySQL does when ending a query with \G on the command line? For instance, "select * from sometable\G" as opposed to "select * from sometable;"
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.Sign up to join this community
Actually, you can enable \G mysql style formatting on a per-query basis in psql by putting the following in ~/.psqlrc:
\set G '\\set QUIET 1\\x\\g\\x\\set QUIET 0'
You can then use :G at the end of a query in the psql client the same as you would \G at the end of a query in the mysql client:
pagila=# select * from foo limit 1; id | uniq | val | val_ts ----+------+-----+--------------------- 1 | 1 | 3 | 2007-07-03 00:00:00 (1 row) Time: 11.481 ms pagila=# select * from foo limit 1 :G -[ RECORD 1 ]--------------- id | 1 uniq | 1 val | 3 val_ts | 2007-07-03 00:00:00
Borrowing from this answer:
You can append
\x\g\x to the query just as you would with MySQL's
\G. For example:
select * from users \x\g\x
Note the lack of
; in the above line, this is intentional.
This has the effect of enabling expanded display, running the expressed query, and disabling expanded display, all in one statement.
Since PostgreSQL 10,
\gx which is the exact equivalent of mysql's
select * from sometable \gx
\gx [ filename ]
\gx [ |command ]
\gx is equivalent to \g, but forces expanded output mode for this query. See \x.
\x\g\x does the same, but only when
\x (=expanded display) is off. Otherwise it does the opposite! By contrast
\gx always displays with the expanded output independently of the current regime of