Postgresql – POSTGRES DB Server Blunder

postgresqlrecovery

This weekend I had to rebuild my laptop which had Postgres running. For some reason, I forgot to dump the data but I did make a complete copy of the original hard drive.

Is it possible to recover data from the original data files to transfer to another instance of Postgres?

Best Answer

Yes, it's fairly simple. You need to copy the database from the old disk image to a directory in the new host then start it using the same major version of PostgreSQL compiled with the same options (integer datetimes on/off, etc).

You can start the DB with a command like PGPORT=5433 pg_ctl -D /path/to/copy/of/old/database start. You must use the same PostgreSQL major version that the DB was created with before. If you're not sure what that was, check the PG_VERSION file in old data directory.

Once you've started a server with the old database you can then pg_dump the databases(s) and pg_dumpall --globals-only the users, etc, and load them into the new database.

Do not try to start the database directly on the old disk image, make a copy and start the copy.

You haven't provided details about versions, operating systems, how you installed Pg on the old and new machines, etc, so it's hard to provide more detail.