'How to determine the total freespce of the db PostgreSQL

How to identify the total size of the db (Used) and the total size of the db.

for the total size of the db (Used) : pg_database_size('dbName') cmmnd works.

But I am not sure how to calculate the free space size of the db. (total capacity I mean)

I saw pg_spacefree('table'). but it would need GRANT access. Is there any other way?

please guide me here?



Solution 1:[1]

As I mentioned in the comment you can create your own function in an untrusted language, for example, plpython

CREATE EXTENSION plpython3u;
CREATE TYPE disk_use AS (Total_GB numeric , Used_GB numeric, free_GB numeric); 
CREATE OR REPLACE  FUNCTION fn_disk_use(part text default '/') RETURNS disk_use AS
$$
  import shutil
  hdd = shutil.disk_usage(part)
  return (hdd.total / (2**30), hdd.used / (2**30),hdd.free / (2**30))
$$ LANGUAGE plpython3u;

 SELECT * from fn_disk_use('/');
     total_gb      |      used_gb      |      free_gb       
-------------------+-------------------+--------------------
 455.2938232421875 | 402.6443672180176 | 29.455318450927734
(1 row)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1