2

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 [email protected]

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.

Colin 't Hart
9,51015 gold badges37 silver badges44 bronze badges
asked Apr 22, 2018 at 18:19

1 Answer 1

1

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.

answered Apr 24, 2018 at 2:04
1
  • This was a minor upgrade. Commented Apr 24, 2018 at 5:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.