← MacSELECT * FROM Shiraverse · All Nukte
Get startedSetupMac › Manual › PostgreSQL

Install PostgreSQL on Mac (step by step)

Homebrew installs PostgreSQL. We link psql, start the database, and create a postgres user and a practice database.

Jo video mein bola, yahan nukte mein likha hai.
What I said in the video, written here as key points.

▶ Watch the video

Mac › Manual › PostgreSQL

Install PostgreSQL

PostgreSQL logo
PostgreSQLthe database engine
⌨️
psqlthe command line tool
🧪
practiceyour own database

Yahan hamara data rehta hai. This is where our data will live.

PostgreSQL is the database engine, the place where your data lives. Homebrew installs it, and we set up two more things: a user called postgres and an empty database called practice, so the same connection details work on every computer in this course.

Step 1

Install and link it

Terminal– □ ×
~ % brew install postgresql ==> Installing postgresql@<version> ~ % brew link --force postgresql@<version> Linking... done

Link zaroori hai. Linking puts psql on your PATH.

Install PostgreSQL:

brew install postgresql

Homebrew installs it as postgresql@<version> and says it is keg-only, which means psql is not on your PATH yet. Link it, using the version number Homebrew printed:

brew link --force postgresql@<version>

Then open a new Terminal window and check psql --version.

Step 2

Start the database

Terminal– □ ×
~ % brew services start postgresql@<version> ==> Successfully started postgresql@<version> ~ % psql --version psql (PostgreSQL) <version>

The database is a background service. Starting it keeps it running.

Start the database as a background service, so it also starts when you log in:

brew services start postgresql@<version>
psql --version

You should see a version number.

Step 3

Create the postgres user

Terminal– □ ×
~ % createuser -s postgres ~ % psql -d postgres -c "ALTER ROLE postgres WITH PASSWORD 'postgres';" ALTER ROLE ~ % createdb -O postgres practice

Yeh user hum khud banate hain. Homebrew does not create it, so we do.

Homebrew makes a super user named after your Mac account, not postgres. Our course, and pgAdmin, use postgres, so we create it:

createuser -s postgres
psql -d postgres -c "ALTER ROLE postgres WITH PASSWORD 'postgres';"
createdb -O postgres practice

The first line makes the user, the second gives it the password postgres (fine for practice on your own Mac), and the third makes the empty practice database.

Step 4

Check you can connect

Terminal– □ ×
~ % psql -h localhost -U postgres -d practice Password for user postgres: practice=# practice=# \q

See practice=#? You are connected. Mubarak ho!

Type:

psql -h localhost -U postgres -d practice

Enter postgres as the password. If you see practice=#, you are connected. Type \q to leave.

If something goes wrong

Common problems

psql: command not foundRun brew link --force postgresql@<version>, then open a new Terminal window.
role "postgres" does not existRun createuser -s postgres (step 3).
PostgreSQL will not startbrew services restart postgresql@<version>
Connection refusedCheck it is running: brew services list.

Tell me exactly what the screen says. Ghabrana nahi.

  • psql: command not found: run brew link --force postgresql@<version>, then open a new Terminal window.
  • role "postgres" does not exist: run createuser -s postgres.
  • PostgreSQL will not start: brew services restart postgresql@<version>.
  • Connection refused: check it is running with brew services list.
Your turn

Did you see practice=#?

sql.theshiraverse.com

Tell me in the comments. Milte hain agli video mein. Next: install pgAdmin 4.

Free SQL course and the Setup page, which maps every step. Join the WhatsApp Community, the Channel or Discord.

→ / Space next   ← back   F fullscreen   H hide bars   N notes