How to relocate tablespace directory
I’ll demonstrate how to relocate a tablespace directory without the reconstruction of databases.
I have a tablespace tblspc
located at /home/postgres/tblspc
and want to relocate to /home/postgres/new_tblspc
.
testdb=# SELECT oid,spcname FROM pg_tablespace WHERE spcname = 'tblspc';
oid | spcname
-------+----------
24580 | tblspc
(1 row)
[postgres]$ pg_ctl -D $PGDATA stop
[postgres]$ cp -r /home/postgres/tblspc /home/postgres/new_tblspc
[postgres]$ rm -rf /home/postgres/tblspc
[postgres]$ cd $PGDATA/pg_tblspc
[postgres]$ rm 24580
[postgres]$ ln -s /home/postgres/new_tblspc 24580
[postgres]$ pg_ctl -D $PGDATA start
Then, the tablespace’s directory has changed.
testdb=# SELECT pg_tablespace_location(24580);
pg_tablespace_location
--------------------------
/home/postgres/new_tblspc
(1 row)