[Ruby] Beginners write! Install postgreSQL on macOS catalina and use it with rails

1 minute read

I am new to programming and mac. I didn’t understand, but it took me a lot of time to research various things, so I decided to write it as a memorandum just in case, It would be really helpful if the expert could point out any mistakes. m(-_-)m Even non-experts will respond as much as possible if there is a consultation such as being unable to do so. *As much as possible^^; Thank you.

First, install postgresql with homebrew and initialize

Run the following in the terminal: The place seems to be good anywhere. I ran it in the projects folder I created earlier. All subsequent work is done at the terminal unless otherwise noted.

brew install postgresql

And the following seems to be the default settings.

initdb /usr/local/var/postgres -E utf8

Start and check the settings

First start.

brew services start postgresql

It seems OK if Encoding is UTF8 below. Maybe I don’t have to do the initial setting? ?

psql -l

And the end.

brew services stop postgresql

Here is the setting that makes postgre easy to use (it seems to be).

I edited .zshrc under the home folder with vscode. Specifically, add the following.

export PATH="/usr/local/opt/postgres:$PATH"
export PGDATA="/usr/local/var/postgres"

Then run the following from the terminal to start postgre.

source ~/.zshrc # Adapt the changed settings.

LOG: ~ LOG: ~ LOG: ~ It seems that it is OK if it is a screen like this. Stop with control+c.

Here is the rails settings.

Please use rails new command to create your favorite app. However of course Don’t forget -d postgresql ^^

Setting database.yml in config folder and how to create user with database creation authority

  • is your favorite name. I made it root this time.
# in database.yml
default: &default
  adapter: postgresql
  encoding: utf8 Changed to #utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") {5} %>
  username: <any username> # add
  password: #add
  host: localhost # add
# Terminal
brew services start postgresql # start first
createuser <any username> -d
# When deleting, dropuser <arbitrary username>

Once you’ve done that, I think you can do it with rails db:create (in any folder of course).

Postico was easy to use for me. I use it for free, but it’s not so expensive You may buy it ^^ The completely free PSequel didn’t work for me ^^;