brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: psql -E shows the SQL behind \d commands, and noexec only shows it

Database
~/tips/tip-database-psql-echo-hidden-flag

psql -E -d mydb

Prints the SQL that psql sends for backslash commands like \dt.

Ever wondered what query hides behind \dt or \d+? psql will tell you, and the SQL it prints is a free lesson in the system catalogs.

Start psql with -E (long form --echo-hidden). It echoes the actual queries generated by \d and other backslash commands, according to the docs, and it is the same as setting the variable ECHO_HIDDEN to on.

psql -E -d mydb
mydb=> \dt

Before the list of tables, you now see the query psql sent to the server. Copy it into your own script or tweak it for a report.

You can also switch it on inside a session:

\set ECHO_HIDDEN on

And there is a third value. When the variable is noexec, the queries are shown but not sent to the server. That is the safe way to study what a command would do. The default value is off.

Caveat: the output of those catalog queries is long and not always pretty, so use it for learning, not for a chat window you want to keep tidy. With noexec you only see the query text and get no result back.

Further reading: psql (PostgreSQL documentation)

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link