[PostgreSQL] Installing PostgreSQL through Homebrew on MacOS
Tags: webdev, database, beginners
Published: 2023-04-22 04:17:01

## 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](https://dba.stackexchange.com/a/3008) or [the installation instruction provided by the Prisma team](https://www.prisma.io/dataguide/postgresql/setting-up-a-local-postgresql-database#setting-up-postgresql-on-macos) 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
```

(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
```


But on the matter of running the server, I found this [stackoverflow link](https://stackoverflow.com/questions/7975556/how-can-i-start-postgresql-server-on-mac-os-x) 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
```

Then how do we create such a superuser `postgres`? I found this [stackoverflow link](https://stackoverflow.com/questions/15301826/psql-fatal-role-postgres-does-not-exist), 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
```

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.