Upgrading postgreSQL with Homebrew

After upgrading a few programs including postgres when starting a new project, I ran into an issue. I already had some postgres databases from older projects and I couldn't start postgres (and create my new database).

To install postgres with brew I used:

$ brew install postgres

To see if postgres is running I can type:

$ brew services 
Name       Status  User Plist
postgresql stopped

Then the funny thing was when I tried to start the service:

$ brew services start postgres
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Great! So it's running! But no..

$ brew services
Name       Status  User             Plist
postgresql error   geoffroy_baumier /Users/geoffroy_baumier/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

To see what is the issue, let's look at the logs:

$ tail /usr/local/var/log/postgres.log
2021-01-26 21:12:47.678 CET [8757] FATAL:  database files are incompatible with server
2021-01-26 21:12:47.678 CET [8757] DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.1.

And there was the issue.
All I had to do to fix this was to run this command:

$ brew postgresql-upgrade-database

Then let's start it postgres:

$ brew services start postgres
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

$ brew services
Name          Status  User             Plist
postgresql    started geoffroy_baumier /Users/geoffroy_baumier/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
postgresql@12 stopped

Now I can see that I have two postgres services with the two different versions. I can create my new updated database. Also the good thing is that I didn't lose any data from previous project and I can run the service with version 12 if I ever need it.