Skip to content

Instantly share code, notes, and snippets.

@MFRWDesign
Last active December 10, 2018 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MFRWDesign/26be786c9c614fec0e55a291e00748c0 to your computer and use it in GitHub Desktop.
Save MFRWDesign/26be786c9c614fec0e55a291e00748c0 to your computer and use it in GitHub Desktop.
This should be of help for working with different versions of postgres and for working with postgres installed on a non-standard port with Rails.
Postgres commands:
pg_wrapper pgql -U JohnDoe -d postgres ---> login
OR
psql --cluster 10/karma -U JohnDoe -d postgres
\l ---> list databases
\du ---> list roles
\dn ---> list schemas
\dt ---> list tables
\q ---> quit
0. Uninstall the Homebrew core postgres package:
brew uninstall postgresql
brew uninstall --force postgresql
(this is exactly equivalent to, and an alias of, brew remove postgresql OR brew rm postgresql)
1. Make sure the postgres brew package from petere is installed with:
brew tap petere/postgresql
(Github repo link: https://github.com/petere/homebrew-postgresql)
NOTE: The repo has a pretty good README
2. Pin the petere postgres brew package to make sure we use it instead of the homebrew core version (to avoid any potential conflicts):
brew tap-pin petere/postgresql
3. Install the version of postgres you want:
brew install postgresql-10
NOTE: Not all minor versions are available. brew install postgresql-10.3 will not work. Available versions can be found listed in the github repo for petere/postgresql: https://github.com/petere/homebrew-postgresql
4. Create the new database cluster for your new version of postgres:
pg_createcluster 10 karma
NOTE: Since installing postgresql@10 actually installed version 10.6, I tried this command with 10.6, but that didn't work giving the error message: "Error: no initdb program for version 10.6 found". This must mean that you can only create clusters matching the homebrew postgres formulas that you have installed.
NOTE 2: The port is automatically assigned and will be incremented by 1 when you install, starting with 5432. Since this was my second installation of postgres, the port 5433 was assigned. This means that database.yml will have to be updated to specify this port.
5. List your postgres clusters:
pg_lsclusters
6. Start your new postgres cluster:
pg_ctlcluster 10 karma start
(and list it again by running pg_lsclusters to see that it is successfully online)
7. As mentioned above, for rails, change the port option in database.yml:
a. If you are using environment variables properly in your database.yml (such as port: <%= ENV.fetch('PG_PORT', 5432) %>), you can simply add the following to your .env file:
export PG_PORT=5433
b. Otherwise, directly edit your database.yml with the following for your development block:
```
development:
...
port: 5433
...
```
8. You will also need to change the settings for your default environment in database.yml, which is used for rake commands like db:drop, etc:
```
default: &default
...
port: <%= ENV.fetch('PG_PORT', 5432) %>
...
```
9. pg_restore still would not run without passing in a `-p 5433`. When using some commands it may be necessary to pass in this value for port if you are using the version of postgres that is not running on the 5432 port.
10. You will also have to add the setting for your test environment in database.yml, as well as adding the environmental variable to your env.test (if you are using it):
```database.yml
test:
...
port: <%= ENV.fetch('PG_PORT', 5432) %>
...
```
```.env.test
export PG_PORT=5433
```
Bonus: Here's a link with some pretty decent info (though it's not 100% correct/complete):
https://medium.com/keeping-code/running-multiple-postgresql-versions-simultaneously-on-macos-linux-90b3d7e08ffd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment