How to Install PostgreSQL on Mac: Step-by-Step Guide
To install
PostgreSQL on a Mac, use the Homebrew package manager by running brew install postgresql in the Terminal. After installation, start the PostgreSQL service with brew services start postgresql and verify it with psql --version.Syntax
Use Homebrew commands in the Terminal to install and manage PostgreSQL on Mac.
brew install postgresql: Installs PostgreSQL.brew services start postgresql: Starts PostgreSQL as a background service.psql --version: Checks the installed PostgreSQL version.
bash
brew install postgresql brew services start postgresql psql --version
Output
psql (PostgreSQL) 15.3
Example
This example shows how to install PostgreSQL, start the service, and check the version to confirm installation.
bash
brew install postgresql brew services start postgresql psql --version
Output
==> Downloading https://homebrew.bintray.com/bottles/postgresql-15.3.catalina.bottle.tar.gz
==> Pouring postgresql-15.3.catalina.bottle.tar.gz
==> Caveats
To have launchd start postgresql now and restart at login:
brew services start postgresql
Or, if you don't want/need a background service you can just run:
pg_ctl -D /usr/local/var/postgres start
psql (PostgreSQL) 15.3
Common Pitfalls
Common mistakes when installing PostgreSQL on Mac include:
- Not having Homebrew installed before running the install command.
- Forgetting to start the PostgreSQL service after installation.
- Confusing
psqlcommands before the service is running. - Not updating Homebrew before installation, which can cause outdated versions.
Always run brew update before installing to avoid version issues.
bash
Wrong way: brew install postgresql psql --version Right way: brew update brew install postgresql brew services start postgresql psql --version
Quick Reference
| Command | Description |
|---|---|
| brew update | Update Homebrew to the latest version |
| brew install postgresql | Install PostgreSQL package |
| brew services start postgresql | Start PostgreSQL service in background |
| psql --version | Check installed PostgreSQL version |
| brew services stop postgresql | Stop PostgreSQL service |
Key Takeaways
Use Homebrew to easily install PostgreSQL on Mac with simple commands.
Always run 'brew update' before installing to get the latest version.
Start the PostgreSQL service after installation to use the database.
Verify installation by checking the PostgreSQL version with 'psql --version'.
Avoid skipping service start or missing Homebrew installation to prevent errors.