Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql find total disk space used by a database

I have more than 50 databases hosted in my postgresql server. I need to move some of them on another host, to free-up some disk space,but how can I measure the disk-space used by each database on my volume?

Is there any function exists to get the information that I want?

like image 951
pylover Avatar asked Jan 15 '13 20:01

pylover


People also ask

How do I check disk space in PostgreSQL?

Here has a simple way to get free disk space without any extended language, just define a function using pgsql. Using df $PGDATA | tail -n +2 instead of df | tail -n +2 while you saving all data in same path on disk. In this case, the function only return one row disk usage for $PGDATA path.

How much disk space does PostgreSQL need?

Introduction. IFI recommends 6TB of disk space for an on-site PostgreSQL instance. Normally, this will accommodate approximately 3 years of database growth (depending on your subscription level).

What is Pg_size_pretty?

pg_size_pretty(numeric) text. Converts a size in bytes expressed as a numeric value into a human-readable format with size units. pg_table_size(regclass) bigint.

What is PostgreSQL vacuum?

Description. VACUUM reclaims storage occupied by dead tuples. In normal PostgreSQL operation, tuples that are deleted or obsoleted by an update are not physically removed from their table; they remain present until a VACUUM is done.


3 Answers

SELECT pg_database_size('geekdb')

or

SELECT pg_size_pretty(pg_database_size('geekdb'))

http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-commands-with-examples/

like image 186
Robert Harvey Avatar answered Oct 29 '22 03:10

Robert Harvey


You could use postgresql Meta-Commands:

  • \l would list databases
  • \l+ extends list with Size, Tablespace, Description.

Use \? to get full list of meta-commands. Also see: https://www.postgresql.org/docs/9.5/static/app-psql.html

like image 37
sharez Avatar answered Oct 29 '22 01:10

sharez


This is an old question, but I created a way to see the results of linux command df -h (Filesystem, Size, Used, Avail, Use%, Mounted on) via a sql query, thus your free disk space and total available disk space for a given file system. Not exactly what the question is about, but helpful for some of use/me. I wish that answer was here hours ago, so I am putting it here (linux only):

create a cron job like this:

@hourly df -h | awk '{print $1","$2","$3","$4","$5","$6}' > /pathhere/diskspaceinfo.csv`

create a foreign table to query:

create extension file_fdw;

create server logserver FOREIGN DATA WRAPPER file_fdw;

CREATE FOREIGN TABLE diskspaceinfo 
(file_sys text, size text, used text, avail text, used_pct text, mount text) 
SERVER fileserver 
OPTIONS (filename '/pathhere/diskspaceinfo.csv', format 'csv');

Then query your table like this:

select * from diskspaceinfo

If you just want something specific, of course just filter the table for what you want. It has limitations, but is very useful for me.

If you have plperlu, you could use this function: https://wiki.postgresql.org/wiki/Free_disk_space

A useful link: https://wiki.postgresql.org/wiki/Disk_Usage

like image 6
mountainclimber11 Avatar answered Oct 29 '22 02:10

mountainclimber11