DEV Community

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

Posted on

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.

Top comments (1)

Collapse
 
sammiee profile image
sammie

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