Search code examples
javapostgresqlfilepg-dumppg-restore

How to store a small piece of information in a file without messing with its content?


My question:

How to store the current version of my software in a dump file generated by PostgreSQL?


The reason for my question:

I've developed a JAVA software using the PostgreSQL database. The software is installed locally on each user's computer, and the database is also local and individual for each user.

I've created a feature so that users can back up their databases and restore them. For this, my JAVA code runs pg_dump to generate the backup file and pg_restore to restore it. That is, the backup is nothing more than a dump of the database generated by the command below:

pg_dump.exe -U myuser -h localhost -p 5432 -Fc -f bkpname.bkp mydb

The problem is that I usually launch software updates. New versions of the software are always compatible with dumps from previous versions. However, older versions of the software are not compatible with dumps generated by a newer version.

Sometimes it happens that a user attempts to restore a recent version of dump in an old version of the software, which is not compatible.

I would like the dump file to have the information of which version of the software generated it. In this way, I could simply display a message informing the user that he needs to download the most current version of the software in order to restore the backup.


I thought of the two forms below, but I think they are not appropriate:

  1. To save the software version to the dump file name. It would be bad because the user could rename the file.
  2. To concatenate the version inside the dump file content. I'm afraid that the dump file might somehow be corrupted in the process of entering text inside it or in the process of removing text of it (before restoring the dump).

Is there a better way to add this information to the dump file?


Solution

  • One idea would be to store the information in a special table inside the database.

    The table is not used normally, and you write the correct version into it right before you perform a dump.

    Before you restore the whole dump, you first restore only that table:

    pg_restore --table dump_version -d mydatabase dumpfile.dmp
    

    Then you check what is in the table and proceed accordingly.