2

I would like to know how to run \du within a script, and output that to a file.

Something like the following:

# some_script.sh

psql <database connection> & \du > output_of_du.txt

I can find information about running .sql files via psql, but I can't find information about running psql commands themselves.

I found the following whilst searching:

But they're not what I'm after.

1 Answer 1

4

You can use the -c flag and combine it with -L to gather the output:

psql <database connection> -c \du -L output_of_du.txt

See psql-tip number 1 (https://psql-tips.org/psql_tips_all.html#tip001) and psql-tip number 26 (https://psql-tips.org/psql_tips_all.html#tip026)

2
  • 2
    Depending on the shell, the backslash will need to be itself escaped (\\du), or the whole argument quoted (‘\du’). Commented Nov 21, 2022 at 15:15
  • 1
    Woops, yes that's right Commented Nov 22, 2022 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.