Question

I had PostgreSQL 9.4.5 installed on MacOS which was done using Homebrew. I wanted to update it to the latest minor version of 9.4, so I did this (if I remember correctly):

brew uninstall postgresql
brew install postgresql@9.4

which installed PostgreSQL 9.4.17 fine, however it seems I lost all my data during the process. I saw initdb was run at one point, is it what caused the deletion?

Note: It was not a production database, so nothing was really lost however I was surprised with this outcome, so I would like to know the reason.

Was it helpful?

Solution

Indeed, you should not have uninstalled it and re-installed it, as PostgreSQL initdb starts a new database store from scratch, hence empty.

BTW it may happen that your data is still there, but in another old directory. See for example this article (https://keita.blog/2016/01/09/homebrew-and-postgresql-9-5/) that shows an upgrade between 9.4 and 9.5 but it may apply in the same way in your case.

If you look at the PostgreSQL formula you can see:

def post_install
    (var/"log").mkpath
    (var/name).mkpath
    unless File.exist? "#{var}/#{name}/PG_VERSION"
      system "#{bin}/initdb", "#{var}/#{name}"
    end
end

so initdb should be called only if directory is not already populated, so in your case the database just got initialized in a new directory, empty, while your data is still in another directory.

But things have changed recently.

Homebrew 1.5, released on January 19th 2018 has this in its changelog:

brew postgresql-upgrade-database is a new command to simplify upgrading PostgreSQL databases between major versions.

It is a wrapper around PostgreSQL pg_upgrade tool, that can be used to upgrade a database in place, hence not loosing anything, including between major versions.

This article (https://olivierlacan.com/posts/migrating-homebrew-postgres-to-a-new-version/) can give you a lot of information on how to upgrade PostgreSQL, and what happens when you use brew upgrade.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top