DEV Community

Cover image for Setting up Postgres via Homebrew
Ryan Glass
Ryan Glass

Posted on

8 1

Setting up Postgres via Homebrew

Setting up PostgreSQL using Homebrew is quick easy and painless, Here's a detailed guide to help you through the process:

Install Homebrew if you haven't already installed Homebrew, you can do so by running the following command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

Installing PostgreSQL once Homebrew is installed, you can install PostgreSQL by running:

brew install postgresql@version_number
Enter fullscreen mode Exit fullscreen mode

Initializing a Database Directory after installation, you can initialize the directory with this command:

brew services start postgresql@version_number
Enter fullscreen mode Exit fullscreen mode

Check that PostgreSQL Services are running with:

brew services list
Enter fullscreen mode Exit fullscreen mode

Creating a Superuser

To create a Superuser, you can access the PostgreSQL interactive terminal by running the command:

psql postgres
Enter fullscreen mode Exit fullscreen mode

Then, you can create a new superuser ideally you would want to replace username with your desired username:

CREATE ROLE username WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'password';
Enter fullscreen mode Exit fullscreen mode

Make sure you replace 'password' with a your super secret secure password.

To exit the PostgreSQL Interactive Terminal: Type \q and then press Return/Enter to quit the PostgreSQL interactive terminal.

But, WAIT.. There's more...

Additionally, If you have multiple versions of PostgreSQL installed, or if you want to use the PostgreSQL@version_number that you installed as your default version, you can link it with:

brew link postgresql@version_number --force
Enter fullscreen mode Exit fullscreen mode

Finally you want to verify the installation, you can verify that PostgreSQL is properly installed and the superuser is created by logging into PostgreSQL with the new user credentials:

psql -U username -d postgres
Enter fullscreen mode Exit fullscreen mode

Again remember to replace username and password with your chosen username and password. Make sure to keep your password secure and avoid using simple or predictable passwords.

I like to use sha256 myself for passwords, but that's an article for another time.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (2)

Collapse
 
zoeedwards profile image
Zoe Edwards

Thanks for posting this Ryan! Tried a couple of guides and this is the one that worked for me. I also found the install had already made me a user with my macOS username.

Quite a lot of guides seem to suggest using sudo, but I found that wasn’t needed as per your guide.

Collapse
 
sammiee profile image
sammie

Thanks for sharing.
I prefer using local server environment because it helps me shorten my development time.

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay