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)"
Installing PostgreSQL once Homebrew is installed, you can install PostgreSQL by running:
brew install postgresql@version_number
Initializing a Database Directory after installation, you can initialize the directory with this command:
brew services start postgresql@version_number
Check that PostgreSQL Services are running with:
brew services list
Creating a Superuser
To create a Superuser, you can access the PostgreSQL interactive terminal by running the command:
psql postgres
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';
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
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
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)
Thanks for sharing.
I prefer using local server environment because it helps me shorten my development time.