Search code examples
databasepostgresqlmigration

Can I import the full data folder from an older PostgreSQL major version to a newer version with only the newer installed?


Assume I have an older major PostgreSQL version (f.e. 15.4) already uninstalled and only the data folder left behind. Can I import that data folder into a newer major version (f.e. 16.3) with only the newer version being installed? If so, how?


For minor versions (f.e. 16.1 -> 16.3) it is possible to simply use the same data folder. (according to the documentation here)

For major versions the "traditional method for moving data to a new major version is to dump and restore the database" (according to the same docu link). It also says that "it is recommended that you use the pg_dump and pg_dumpall programs from the newer version of PostgreSQL", but how can I call pg_dumpall on the old data folder to create a dump that I can import into the new installed version? With the -d or -l parameters? Or would the PostgreSQL server instance first have to point to the old data folder (which I assume does not always work) before I can use pg_dumpall on it?

The other recommended option is pg_upgrade, but it seems to require the old PostgreSQL executable directory (-oldbindir parameter), so I assume I cannot use that tool with only the newer major version installed.


Solution

  • The data files of PostgreSQL major version are not compatible. To use your PG15 data in a version 16 server you'll need to re-install version 15, start a postgres-15 server with the old data directory and either:

    1. Perform a logical upgrade (i.e. pg_dump) to get a dump of the old instance in a sql file, then import that file in a running PG16 instance (I suggest to use pg_dump version 16 to dump the data from 15, as per best-practice)

    It is recommended that you use the pg_dump and pg_dumpall programs from the newer version of PostgreSQL, to take advantage of enhancements that might have been made in these programs. Current releases of the dump programs can read data from any server version back to 9.2.

    1. Use pg_upgrade, which also requires having both version 15 and 16 installed