DEV Community

Cover image for ๐Ÿ˜ Simplified Guide to Setting Up PostgreSQL on macOS with Docker ๐Ÿšข
kpndevroot
kpndevroot

Posted on

๐Ÿ˜ Simplified Guide to Setting Up PostgreSQL on macOS with Docker ๐Ÿšข

Are you struggling with PostgreSQL configuration on macOS?

If youโ€™ve been wrestling with environment variables like these:

DATABASE_URL=postgres://myuser@host.docker.internal:5432/mydb
#OR
DATABASE_URL=postgres://myuser:myuser@127.0.0.1:5432/mydb
Enter fullscreen mode Exit fullscreen mode

Youโ€™re not alone! PostgreSQL setup on macOS can feel daunting due to:

  • โŒ Limited client tools: Unlike Linux, macOS lacks straightforward commands like createuser --interactive -P.
  • ๐Ÿ“– Sparse documentation: Most PostgreSQL guides cater to Linux or Windows, leaving macOS users to figure it out on their own.

But donโ€™t worryโ€”weโ€™ll walk through an easy and beginner-friendly way to set up PostgreSQL on macOS using Homebrew and Docker!๐ŸŽ‰

๐Ÿ† Why This Guide Stands Out

  • โœ… Clear step-by-step instructions.
  • ๐Ÿ–ฅ๏ธ Tailored for macOS users.
  • ๐Ÿ“Š Real-world examples with expected outputs for verification.

Letโ€™s dive in! ๐Ÿš€

๐Ÿ”ง Step 1: Install PostgreSQL Using Homebrew

1๏ธโƒฃ Install PostgreSQL with Homebrew:

brew install postgresql@16
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ Start the PostgreSQL service:

brew services start postgresql@16
Enter fullscreen mode Exit fullscreen mode

3๏ธโƒฃ Verify the service status:

brew services info postgresql@16
Enter fullscreen mode Exit fullscreen mode

You should see output like this:

postgresql@16 (homebrew.mxcl.postgresql@16)
Running: โœ”
Loaded: โœ”
Schedulable: โœ˜
User: root
PID: 38646
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Pro Tip
If you encounter issues, restart the service:

brew services restart postgresql@16
Enter fullscreen mode Exit fullscreen mode

๐Ÿ—๏ธ Step 2: Create a PostgreSQL User and Database

1๏ธโƒฃ Access the PostgreSQL interactive shell:

psql postgres
Enter fullscreen mode Exit fullscreen mode

Example output:

โฏ psql postgres
psql (14.13 (Homebrew), server 16.4 (Homebrew))
WARNING: psql major version 14, server major version 16.
         Some psql features might not work.
Type "help" for help.
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ Create a User and Database

  1. Create a user
CREATE USER myuser WITH PASSWORD 'user';
Enter fullscreen mode Exit fullscreen mode

Expected Output:

CREATE ROLE
Enter fullscreen mode Exit fullscreen mode
  1. Create a database:
CREATE DATABASE mydb;
Enter fullscreen mode Exit fullscreen mode

Expected Output:

CREATE DATABASE
Enter fullscreen mode Exit fullscreen mode
  1. Grant access:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Enter fullscreen mode Exit fullscreen mode

Expected Output:

GRANT
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”— Step 3: Connect to Your Database

Now, letโ€™s test the connection to ensure everything is working. Use the following command to connect:

psql -U myuser -d mydb -p 5432
Enter fullscreen mode Exit fullscreen mode

Expected Output:


โฏ psql -U myuser -d mydb -p 5432

psql (14.13 (Homebrew), server 16.4 (Homebrew))
WARNING: psql major version 14, server major version 16.
         Some psql features might not work.
Type "help" for help.

mydb=>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ› ๏ธ Common Issues and Solutions

1๏ธโƒฃ Connection Refused

  • Cause: Firewall or incorrect host.docker.internal.
  • Solution: Test with 127.0.0.1 or check your Docker network settings

2๏ธโƒฃ Version Mismatch Warnings

  • Cause: Using an older psql client.
  • Solution: Update your client to match the server version.

๐ŸŽฏ Whatโ€™s Next?

  • ๐Ÿ” Explore your database.
  • ๐Ÿ› ๏ธ Start building applications.
  • โš™๏ธ Configure your DATABASE_URL with host.docker.internal for Docker-based projects.

Now youโ€™re ready to handle PostgreSQL on macOS like a pro! ๐Ÿš€

If you enjoyed this guide, donโ€™t forget to share it with your fellow developers! ๐Ÿง‘โ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

Drop a comment below if you have questions or tips to add. Letโ€™s make PostgreSQL configuration seamless for everyone! ๐ŸŒŸ

Top comments (0)