DEV Community

UponTheSky
UponTheSky

Posted on • Edited on

[PostgreSQL] Installing PostgreSQL through Homebrew on MacOS

Why this article?

installing a PostgreSQL application on my Mac seemed to be a piece of cake until I realized that it is a rather tricky and time consuming task, since many pages on the Internet were not providing comprehensive but only partial guides. The following is a collection of information on installation of a PostgreSQL application on MacOS, from installation using Homebrew, run the server, and use psql commands.

1. Installing PostgreSQL though Homebrew

This part is rather easy: just run

brew install postgresql@15

(I chose version 15 but you can just substitute the number after @ with any version you want, like postgresql@14)

All the story would have been fairly easy if this is the end, but it was the opposite.

2. Find the binary file and include to your PATH

Now many of the instructions on the Internet would just say “run psql foo bar baz …”. However, although I don’t know the exact reason, but only installing the PostgreSQL application using Homebrew didn’t provide such psql command right away.

The stackoverflow answer or the installation instruction provided by the Prisma team were the correct solution in this case. I had to include the path of my binary file into the PATH variable.

This was easy: run brew info postgresql@15 and follow the instruction given. In my case, the instruction was

If you need to have postgresql@15 first in your PATH, run:
echo 'export PATH="/usr/local/opt/postgresql@15/bin:$PATH"' >> ~/.zshrc

So I ran

 echo 'export PATH="/usr/local/opt/postgresql@15/bin:$PATH"' >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

(Or you could directly add the line export PATH="/usr/local/opt/postgresql@15/bin:$PATH to your favorite config file such as .bash_profile).

Now you can run psql and pg_ctl.

3. Run the server

You can just run or stop the server using Homebrew commands:

brew services start postgresql@15
brew services stop postgresql@15
Enter fullscreen mode Exit fullscreen mode

But on the matter of running the server, I found this stackoverflow link which looks quite comprehensive. You may find one that more suits you from the link here.

4. Generate a superuser

However, still you can’t access the server, since you’re not an authenticated user. If you run the following command, you’ll see an error message like “there is no such a role”.

psql -h localhost -U postgres
Enter fullscreen mode Exit fullscreen mode

Then how do we create such a superuser postgres? I found this stackoverflow link, and this provided the answer, that I needed to run the command createuser on the shell directly(this postgres-specific command is available since we already included the binary files to the PATH variable):

createuser -s postgres
Enter fullscreen mode Exit fullscreen mode

Now you can run psql -h localhost -U postgres and access the server!

Wrap Up

It was a bit cumbersome work to do, since this brew install wasn’t that magic command in case of PostgreSQL(It was way more easier in the MySQL case). I hope you could find some help from this article here.

Top comments (4)

Collapse
 
meg_gutshall profile image
Meg Gutshall

Great post! To the point and you cite your sources. Thank you!

Collapse
 
uponthesky profile image
UponTheSky

Thank you for your comment!

Collapse
 
wilmela profile image
Wilmela

Thanks for sharing.

Collapse
 
uponthesky profile image
UponTheSky

Glad that it helps!