How to move Postgre DB from one server to another
I need to run my RoR application on a new server. My application already has some vital data in its DB. I’m using PostgreSQL as DB. All app code is up and in git. There shouldn’t be a problem to deploy it to new place. The weak side is DB. I need to move it unchangebly from one place to another. Here is my steps of how to achive this.
The best way to do it is to backup db on source server and restore it on destination server.
According to official documentation there are 3 ways how to do this. I chose File System Level Backup
way.
Let’s log in to source server and find out place of data directory. On source server I have ubuntu distributive and installed PostgreSQL 9.3. Open /etc/postgresql/9.3/main/postgresql.conf
and find line with data_directory
. My line is data_directory = '/var/lib/postgresql/9.3/main'
.
Backup it via following command:
Log in to destination server and install PostgreSQL.
You may see following message:
Turned out Ubuntu 13.10 (saucy) which is on my desination server doesn’t have 9.3 version. We made backup of 9.3 DB, so we need to restore it in 9.3. Here is workarround:
Create pg.list
file in /etc/apt/sources.list.d
. Add deb http://apt.postgresql.org/pub/repos/apt/ saucy-pgdg main
line to it and make
Now let’s restore DB at new place. Make sure postgresql is stopped
Start postgre service. Now you should have your fully restored DB. Enjoy!