HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to migrate a database from a local to a remote server?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
localservermigratedatabasehowremotefrom

Problem

I have a postgres db that I have created and need to deploy remotely. From reading online I have understood that to do that I need to modify the following files :

-
pg_hba.conf

-
postgresql.conf

From what I read I need to modify them and somehow add them to my database?

My question is:

-
How do I modify the 2 files to deploy a database online? I read the documentation but its too complicated for my understanding.(ie what is needed and why?)

-
How do I add them to my database online? My idea was to create a database dump of the local DB, then push the dump on a new database on the remote computer, but how do these 2 files fit in there? Where should there be placed? I cannot find details on this.

note. I have never done distributed programming before, my question might be naive but I really have no idea how to proceed.

I am using osX operating system,but will be using linux on the remote computer.

Solution

The two configuration files apply to the server, so both your local and remote server already have those files and their contents is specific to the installation.

Assuming your remote server is already up and running, all you need to do is dump the database you want to deploy to a file and then load the dump file in your remote server. You do not have to do anything with the configuration files.

You can use pg_dump to dump the local database to file, doing something like this:

pg_dump -U postgres -Fc my_db > my_db.db


But check all the options of the pg_dump program.

Loading the database on your remote server is just as easy with pg_restore:

pg_restore -h  -p  -U postgres -C my_db.db


On the remote server you need to specify a user who can create databases, if you do not have access to the postgres role.

If you are on some flavour of *nix you can even pipe pg_dump output into pg_restore input and do it all in one go:

pg_dump -U postgres -Fc my_db | pg_restore -h  -p  -U postgres -C


Note that pg_dump will ask for a password on the local server (if at all) and pg_restore will for a password on the remote server (if at all).

Both programs have a lot of options. Be sure to read the documentation fully and use options to match your situation before coming back to tell me that it doesn't work.

Code Snippets

pg_dump -U postgres -Fc my_db > my_db.db
pg_restore -h <ip remote server> -p <port remote server> -U postgres -C my_db.db
pg_dump -U postgres -Fc my_db | pg_restore -h <ip> -p <port> -U postgres -C

Context

StackExchange Database Administrators Q#117629, answer score: 8

Revisions (0)

No revisions yet.