However, it's not infrequently the case that at some point you want to move an existing tablespace to a different mount point, because you're changing storage, re-arranging the filesystem, or making backup easier. There's two different ways to move tablespaces in current PostgreSQL: the slow online way and the fast downtime way.
The slow online way works like this:
- create a new tablespace in the desired new location
- go through all of the tables/indexes stored in the old tablespace, and do ALTER <object> TABLESPACE <new_tablespace> on each of them.
- drop the old tablespace
The second approach requires a database downtime and some understanding of how PostgreSQL stores tablespaces. Note that this technique will not work on versions prior to 9.2, since those versions also store the tablespace path in a system table, which forces you to use the online method.
If you look in your $PGDATA directory, you will find a subdirectory called pg_tblspc. This directory contains entries like this:
lrwxrwxrwx 1 josh josh 26535 -> /home/josh/tblspc
Each listing is a link to a directory location. If you follow that link, you find something like this:
rwx------ 3 josh josh 4096 PG_9.3_201306121
... in the target directory, there is a subdirectory named after the exact version which created it, which actually contains your tablespace data. At this point, a different way to move the tablespace should have occurred to you, and it does work.
- shut down the database system
- move or copy the PG_9* subdirectory from the old tablespace mount to the new one.
- change the link in pg_tblspc to point to the new mount point
- start the database system back up
The reason why this is a "faster" method is that step 2 allows you to use whatever filesystem copying tricks you wish to speed things up, such as a 2-stage rsync. It also allows you to handle cases where both tablespaces won't be online at the same time.
Also note that I'm not sure this works the same way on Windows.
Hope that helps!
Second Way: Need we update any system tables ?
ReplyDeleteThis comment has been removed by the author.
DeleteHi Josh,
ReplyDeletein the old systems (e.g. 9.0) you could:
- shut down the DB
- move tablespace
- fix the sym link in pg_tblspc
once you start the DB, one additional update in pg_tablespace catalog is needed to fix the path (pg_tablespace.spclocation).
The OS is Linux: didn't try this in Windows.
Best,
Milos
This is correct, and should work equally well at Windows. In fact, you don't even "have to" update the spclocation field - PostgreSQL never actually *uses* that. It might be in use by third party tools though, and I think it's in use by pgAdmin, so it's definitely recommended.
DeleteBut the fact that it was never actually used is what made it very easy for me to get rid of it completely, replacing it with the function that inspects the symlink.
Ive followed these tips and I got my tablespaces moved to another location but pgadmin still shows the old location. How can I change this information in 9.2.6?
DeleteThx in adv
Adolfho
This comment has been removed by the author.
DeleteThis worked for me (Windows 7 with Postgres 9.1)
DeleteTo fix the sym link from milosbabics step 3 use windows command promt:
mklink /J C:\Program Files (x86)\PostgreSQL\9.1\data\pg_tblspc\OLDTABLESPACENUMBER PATHTONEWTABLESPACE
In my case OLDTABLESPACENUMBER was 11100853
In PATHTONEWTABLESPACE the was the subfolder:
PG_9.1_201105231\11100854
Mind the two numbers: 11100853 and 11100854 !
I can confirm that WIndows uses the same symlink method as Unix (called junction points).
ReplyDeleteI can confirm this method is working on Windows Server 2k8 with postgres 9.2. And pg admin is showing the good location.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete