How to install PostgreSQL on macOS

PostgreSQL is a powerful open-source relational database. On macOS, the easiest way to install it is with Homebrew.

Prerequisites

Make sure Homebrew is installed on your Mac. You can check it with:

brew --version

If Homebrew is not installed, install it with:

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

Install PostgreSQL

Update Homebrew first:

brew update

Then install PostgreSQL:

brew install postgresql

Start PostgreSQL

After installation, start the PostgreSQL service:

brew services start postgresql

If you want to stop it later, run:

brew services stop postgresql

Verify the installation

Check the installed version:

psql --version

You can also connect to PostgreSQL from the terminal:

psql postgres

If everything is working, you will enter the PostgreSQL shell.

Create a database

Create a new database with:

createdb myapp

Then connect to it:

psql myapp

Useful PostgreSQL commands

Inside the PostgreSQL shell, these commands are helpful:

\l

List all databases.

\c myapp

Connect to a database.

\dt

List tables in the current database.

\q

Quit PostgreSQL shell.

Uninstall PostgreSQL

If you need to remove PostgreSQL later:

brew services stop postgresql
brew uninstall postgresql

Using Homebrew makes PostgreSQL installation and management simple on macOS. Once installed, you can start building apps, creating databases, and running queries locally.

Related posts

Md. Monirul Alom

Md. Monirul Alom

I am a Full Stack Web developer. I love to code, travel, do some volunteer work. Whenever I get time I write for this blog