1

I want to use value returned by psql in bash. This is the script:

psql "connection parameters" -c "SELECT pg_database_size('dbname');"

The output is like this:

 pg_database_size 
------------------
          5773072
(1 row)

But I only want the 5773072 so I can use it in logging. Can anyone help?

2

The -t (--tuples-only) option might be used also:

psql "connection parameters" -t -c "SELECT pg_database_size('dbname');"
2

In addition to what @Milen already provided, you may want -A (--no-align) to remove leading white space:

psql "con params" -tAc "SELECT pg_size_pretty(pg_database_size('mydb'))"

As an aside: pg_size_pretty() may or may not be of interest.

  • I think this is better than the confirmed answer – Brett Cullen Feb 13 '18 at 21:00
0

I got it:

psql "connection parameters" -c "SELECT pg_database_size('dbname');" | tail -3 | head -1 | tr -d ' '

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.