Installing and Setting Up PostgreSQL on MAC with Apple Silicon
A Step-by-Step Guide and Troubleshooting Tips
Recently, I was brought on board to contribute to an open-source project called CivicTechJobs that uses a technology stack consisting of Django, Django REST Framework, React, Webpack, PostgreSQL, and Docker. While I'm excited to brush up my Python skills, I feel slightly intimidated as I have limited experience with Postgres or any local databases (cloud databases were my first choice for my solo projects).
After setting up my local environment (forking, cloning the repo, and adding remote upstream; for more information, check out my other blog article), the first step is to run Docker and take a look at the current state of the project.
However, I realized there is a "Step 0," which is to install Postgres and set up a user, password, database, and configure a port for SQL. This step took me a while to complete, so I want to document it in this article, hoping it could help other people facing similar issues.
For this project, I'm using a MacBook Pro with an Apple M1 Pro chip, and I started with Homebrew.
Installing PostgreSQL
Updating Homebrew
First, I used the command
brew -v
to check ifhomebrew
was installed on my computer and, if so, which version. If you don't have Homebrew installed on your Mac, you can follow these instructions to install it.The next step is to update homebrew:
brew -v brew update
Be patient if this step is taking a while. But if it's taking suspiciously long, you can try
brew update --debug --verbose
to get more details.Now, we can use homebrew to install Postgres:
brew install postgresql@<version>
I installed postgresql@14 but you can check the versions and notes for reference.
Troubleshooting psql Error:
Everything was looking goodโI saw logs about installing dependencies and info on
postgresql@14
,openssl@3
andsqlite
... However, I encountered an error message:I stopped the installation process by using this command:
brew services stop postgresql
After some Googling, I narrowed the issue down to the
postgresql.conf
file. So, I attempted to locate it in the terminal:/opt/homebrew/var ls
Found
postgresql@14
! ๐ (see screenshot below ๐๐ป)Now let's cd into
postgresql@14
and list all the files:cd postgresql@14 ls
Found
postgresql.conf
! ๐I opened the file in VS Code:
code . postgresql.conf
In the "CONNECTIONS AND AUTHENTICATION" section, I found a line with a commented-out
port = 5432
. I removed the#
symbol to uncomment it, but I also noticed that the comment next to it said# (change requires restart)
. So, as instructed, I saved the change and restarted my computer.
Start the Database
After restarting my computer, on my second attempt, I executed the following command in the terminal:
brew services start postgresql
This time, I saw the message "==> Successfully started
postgresql@14
(label: homebrew.mxcl.postgresql@14)" which indicated that PostgreSQL was successfully installed and running. ๐ฅน
Create a User and Password
Create a Root User
In the terminal, use psql
to connect to the postgres database:
psql postgres
You will see the prompt: postgres=#
Create Role with Password
postgres=# CREATE ROLE <user> WITH LOGIN PASSWORD '<password>';
postgres=# ALTER ROLE <user> CREATEDB;
Quit psql
postgres=# \q
Stop PostgreSQL
brew services stop postgresql
Update Dev Env Values
If you need to set up dev.env in your local repo, you can use the role and password you just created to update these Postgres variables:
# postgres
POSTGRES_DB=postgres
POSTGRES_USER=<user>
POSTGRES_PASSWORD=<password>
This time, my docker composed up with no problem, phew. It felt a little overwhelming at first but I'm glad I did my homework and solved the problem. I hope this article helps you troubleshoot your PostgreSQL installation and set up. I will keep exploring Postgres and will share more tips that I think might be helpful to developers.
Happy coding!
Bitian
Cover Photo by Casey Allen: https://www.pexels.com/photo/grayscale-photography-of-elephants-16023/
Resources:
https://daily-dev-tips.com/posts/installing-postgresql-on-a-mac-with-homebrew/