Fantastic PostgreSQL and how to make it work on MacOS
By: Anastasia Orlova
Tags: Postgresql, Flatiron School, Ruby on Rails, Postgres, Newbie

PostgresSQL logo

As an official website says, PostgreSQL is a powerful, open source object-relational database system. The main pros of using PostgreSQL is pretty obvious: if you want to deploy your amazing project to the web via Heroku, you’d better use it.

However, it might be really challenging to make PostgreSQL work. I’ve tried several ways of installing PostgreSQL (via direct download, gems, etc) but I kept getting this error while trying to init my database.

FATAL: password authentication failed for user "username"

Couldn't create 'new_app' database. Please check your configuration.

rails aborted!

I searched through stack overflow but every single recommendation I tried didn’t work for me. Finally, I ran into this tutorial that was really helpful. Let me walk you through it and show what other errors I got.

Step 0: uninstall the existing PostgreSQL

If you’ve tried to install PostgreSQL and haven’t succeeded, the first thing you have to do is to remove it from your local machine and uninstall the gem.

$ gem uninstall pg

$ brew uninstall postgresql

Sometimes this small step fixes all the issues you encounter!

Step 1: install PostgreSQL and check what version you have

brew update

brew install postgresql

After that make sure that you’re using the latest version of PostgreSQL to avoid further bugs.

postgres --version

Side note: Homebrew package is highly recommended for installing and managing applications on MacOS. Here is an installation command.

Step 2: create a database on your hard disk

As a rule, a database on your hard disk is created automatically but, just to make sure, run this command:

initdb /usr/local/var/postgres

If the database was created, you will see the following error message: “initdb: directory “/usr/local/var/postgres” exists but is not empty”

Step 3: start your PostgreSQL database

You can manually start the database server with this command:

pg_ctl -D /usr/local/var/postgres start

The terminal should send you a response — “server started”.

And here I got another error!

It seems that another database is running on the port that I would like to use. I tried to stop the database and then restart but it didn’t help. Btw, to stop the database you can use this command.

pg_ctl -D /usr/local/var/postgres stop

So, the only way that was left is to kill this beast. I mean, the port.

I used

sudo pkill -u postgres

— which is actually not recommended because you don’t see what port you’re killing. As I found out later, a better practice is to use lsof -wni tcp:5432. The last four digits are the number of the port that you’re up to kill.

Anyway, it worked just perfectly! As you can see below, my database was finally started. Yay!

Step 4: set up a database for your application

Now, when your database is on, you go ahead and can run the classic:

rails new <my-project> --api -T --database=postgresql

rails db:create

rails db:migrate

rails db:seed

Step 5: always check your migration!

Oh, and if you have a moment, please see my project! Now it’s on-air, just click the link.

Source: