13

I am using postgres 9.2 on redhat 6

this should be simple but I can't find it anywhere. I am looking for the database table and column which stores the Location for a postgres tablespace, I thought it would be in PG_TABLESPACE, but

select * from pg_tablespace

shows...

postgres=# select * from pg_tablespace;
     spcname     | spcowner | spcacl | spcoptions
-----------------+----------+--------+------------
 pg_default      |       10 |        |
 pg_global       |       10 |        |
 C_TBL_DB91SABIR |       10 |        |
(3 rows)

but no location, any ideas where the location is kept?

thanks

peterh
  • 11,875
  • 18
  • 85
  • 108
davegreen100
  • 2,055
  • 3
  • 13
  • 24
  • 1
    Not the exact answer : `SHOW data_directory;` will show you the location of current DB – Vivek S. Feb 05 '16 at 10:36
  • hi @wingedpanther, that shows the PGDATA location, but i'm looking for the directory of the tablespace, i want to query it so that i can script a removal of the physical folder when a tablespace is dropped. – davegreen100 Feb 05 '16 at 10:38
  • 3
    Try this `select spcname, pg_tablespace_location(oid) from pg_tablespace; ` – Vivek S. Feb 05 '16 at 10:45

3 Answers3

34

Use pg_tablespace_location(tablespace_oid)(PostgreSQL 9.2+) to get the path in the file system where the tablespace is located.

You'll get oid of tablespace from pg_tablespace, so the query should be

select spcname
      ,pg_tablespace_location(oid) 
from   pg_tablespace;
Vivek S.
  • 19,945
  • 7
  • 68
  • 85
  • 1
    both this sql query and psql command \db+ is relevant with user defined or so called non system tablespaces. e.g default pgroot is defined by environment variable PGROOT on some cases – Mehme Sep 07 '22 at 08:21
5

Another super easy command to list all table spaces

\db+

This will provide you all table space details very quick

Jerry
  • 7,863
  • 2
  • 26
  • 35
-4

Use below query to get tablespace name tablespace location.

postgres=# select spcname ,pg_tablespace_location(oid) from pg_tablespace;
  spcname   |    pg_tablespace_location     
------------+-------------------------------
 pg_default | 
 pg_global  | 
 fastspace  | /var/lib/postgresql/data/base
 demotbs    | /var/lib/postgresql/data
Srikant Patra
  • 399
  • 4
  • 5