Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

(1 row)

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

share|improve this question
up vote 2 down vote accepted

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

psql "connection parameters" -t -c "SELECT pg_database_size('dbname');"
share|improve this answer

In addition to what @Milen already provided, you may want -A (--no-align) 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.

share|improve this answer

I got it:

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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