2

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?

Erwin Brandstetter
186k28 gold badges464 silver badges636 bronze badges
asked Mar 11, 2012 at 11:58

3 Answers 3

3

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

psql "connection parameters" -t -c "SELECT pg_database_size('dbname');"
answered Mar 11, 2012 at 12:57
5

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.

answered Mar 12, 2012 at 6:32
1
  • 1
    I think this is better than the confirmed answer Commented Feb 13, 2018 at 21:00
1

I got it:

psql "connection parameters" -c "SELECT pg_database_size('dbname');" | tail -3 | head -1 | tr -d ' '
answered Mar 11, 2012 at 12:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.