PostgreSQL is a popular open-source relational database management system. Sometimes, when you try to start PostgreSQL, you may encounter an error message that says “An old version of the database format was found.” This error typically occurs when you try to start PostgreSQL with a data directory that was created by an older version of PostgreSQL. In this guide, we will show you how to fix this error and start PostgreSQL successfully.
Step 1: Locate the Data Directory
The first step is to locate the data directory of your PostgreSQL installation. By default, the data directory is located at /var/lib/postgresql/<version>/main/ where <version> is the version of PostgreSQL you have installed. To find out the version of PostgreSQL you have installed, run the following command:
psql --version
This command will display the version of PostgreSQL installed on your system.
Step 2: Backup the Data Directory
Before making any changes, it is important to back up the data directory in case something goes wrong. To do this, make a copy of the data directory and save it in a safe place. Run the following command to make a copy of the data directory:
sudo cp -a /var/lib/postgresql/<version>/main /var/lib/postgresql/<version>/main_backup
Step 3: Upgrade the Data Directory
Now, you need to upgrade the data directory to the current version of PostgreSQL. To do this, run the following command:
sudo pg_upgradecluster <version> main
This command will upgrade the data directory to the current version of PostgreSQL. <version> should be replaced with the version of PostgreSQL you have installed.
Step 4: Start PostgreSQL
Finally, you can start PostgreSQL by running the following command:
sudo service postgresql start
This command will start the PostgreSQL service, and you should be able to connect to your databases without encountering the “An old version of the database format was found” error.
Commands Mentioned:
- psql –version – Find out the version of PostgreSQL installed
- sudo cp -a /var/lib/postgresql/<version>/main /var/lib/postgresql/<version>/main_backup – Make a backup of the data directory
- sudo pg_upgradecluster <version> main – Upgrade the data directory
- sudo service postgresql start – Start the PostgreSQL service
Conclusion
In this guide, we have shown you how to fix the “An old version of the database format was found” error when starting PostgreSQL. By upgrading the data directory to the current version of PostgreSQL, you can start the PostgreSQL service without encountering this error. However, it is important to remember to back up the data directory before making any changes to avoid data loss. If you have any comments or suggestions for improvements, please feel free to share them below.
3 Comments
Hi,
Recently I encountered the same problem when upgraded postgresql from 8.2 to 8.4.
Nice to see your fix solution. It seems that you destroy the database at first and then reinitialize it. My question is, after all the steps you offered, what should we do to recover the original database?
Thanks,
Erming
Might want to take a look at the rm command. rm -rf * is dangerous if running as root!
whart to do when pg_upgradecluster is not found??