DEV Community

Kelvin Wangonya
Kelvin Wangonya

Posted on • Edited on • Originally published at wangonya.com

4 1

What happens when you run PSQL slash commands?

Running \? within a psql database gives a whole list of commands that come in handy when performing various kinds of tasks.

$ psql
psql (13.0)
Type "help" for help.

wangonya=# \?
General
  \copyright             show PostgreSQL usage and distribution terms
  \crosstabview [COLUMNS] execute query and display results in crosstab
  \errverbose            show most recent error message at maximum verbosity

  .....
Enter fullscreen mode Exit fullscreen mode

But what are the queries executed when these commands run?

I was recently tasked to write a bash script that loops through every schema on a database and does a pg_dump on it for backup then uploads the backups to an external server.

Running \dn gives a list of schemas but it also gives the owners, which I didn't need in my case.

wangonya=# \dn                                                             
  List of schemas
  Name  |  Owner   
--------+----------
 public | wangonya
 x      | wangonya
 y      | wangonya
(3 rows)
Enter fullscreen mode Exit fullscreen mode

I only needed the schema names so I had to look for a way to edit the query run by \dn to only return names.

From the psql docs:

-E
--echo-hidden

Echo the actual queries generated by \d and other backslash commands. You can use this to study psql's internal operations. This is equivalent to setting the variable ECHO_HIDDEN to on.

Running psql with the -E flag then running the /dn command gives this result:

$ psql -E

wangonya-# \dn
********* QUERY **********
SELECT n.nspname AS "Name",
  pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner"
FROM pg_catalog.pg_namespace n
WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'
ORDER BY 1;
**************************

  List of schemas
  Name  |  Owner   
--------+----------
 public | wangonya
 x      | wangonya
 y      | wangonya
(3 rows)
Enter fullscreen mode Exit fullscreen mode

With the underlying query executed on running the slash command, I was able to edit it get the desired result.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
mtancoigne profile image
Manuel Tancoigne

Simple and useful!

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay