DEV Community

suman-dangol
suman-dangol

Posted on

Postgres Installation Guide

Step 1: Install Homebrew

If you don't have Homebrew installed on your Mac, open a terminal and run the following command:

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

This script will install Homebrew.

Step 2: Install PostgreSQL via Homebrew

Once Homebrew is installed, you can install PostgreSQL by running:

brew install postgresql
Enter fullscreen mode Exit fullscreen mode

Step 3: Start PostgreSQL

After installation, you can start PostgreSQL using Homebrew services:

brew services start postgresql
Enter fullscreen mode Exit fullscreen mode

This command will also set PostgreSQL to start automatically at login.

Step 4: Create Your Database and User

  1. Access PostgreSQL Prompt:
   psql postgres
Enter fullscreen mode Exit fullscreen mode
  1. Create a Role (User):
   CREATE ROLE myuser WITH LOGIN PASSWORD 'mypassword';
Enter fullscreen mode Exit fullscreen mode
  1. Create a Database:
   CREATE DATABASE mydb OWNER myuser;
Enter fullscreen mode Exit fullscreen mode
  1. Grant Permissions:
   GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Enter fullscreen mode Exit fullscreen mode
  1. Exit psql:
   \q
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify Installation

To verify that everything is set up correctly, try connecting to the database you created:

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

You should be able to log in without errors.

Additional Configuration

You can configure PostgreSQL to listen on different interfaces or to adjust other settings by modifying the PostgreSQL configuration file typically found at /usr/local/var/postgres/postgresql.conf. If you need to allow remote connections, you would also adjust the /usr/local/var/postgres/pg_hba.conf file.

Summary

Now, you should have PostgreSQL installed and running on your macOS system. You can manage PostgreSQL with Homebrew services and access your databases with psql or any other PostgreSQL-compatible tool. If you need any more detailed configuration, like setting up remote access or securing your database, you may need to modify configuration files or apply additional security practices as needed.

Top comments (0)