DEV Community

Cover image for Setting Up PostgreSQL for macOS Users: Step-by-Step Instructions
FOLASAYO SAMUEL OLAYEMI for TechPrane

Posted on

Setting Up PostgreSQL for macOS Users: Step-by-Step Instructions

If you're using macOS, the steps to install PostgreSQL and set up your environment are slightly different. Here's how to do it:

1. Installing PostgreSQL on macOS

There are multiple ways to install PostgreSQL on macOS, but one of the easiest is by using Homebrew, a package manager for macOS.

Step 1: Install Homebrew (If Not Installed)

If you don't have Homebrew installed, open your 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 will install Homebrew on your system.

Step 2: Install PostgreSQL via Homebrew

Once Homebrew is installed, use the following command to install PostgreSQL:

brew install postgresql
Enter fullscreen mode Exit fullscreen mode

Step 3: Start the PostgreSQL Service

After the installation, start the PostgreSQL server:

brew services start postgresql
Enter fullscreen mode Exit fullscreen mode

This will ensure that the PostgreSQL server starts automatically when your macOS system boots up.

Step 4: Verify Installation

Check if PostgreSQL was installed correctly by running:

psql --version
Enter fullscreen mode Exit fullscreen mode

This should display the installed PostgreSQL version.

Step 5: Access PostgreSQL

You can now access PostgreSQL using:

psql postgres
Enter fullscreen mode Exit fullscreen mode

2. Creating a Database for Your Application

Once PostgreSQL is installed, you need to create a database for storing products with JSONB attributes.

Step 1: Create a New Database

Inside the PostgreSQL command-line interface (psql), create a new database:

CREATE DATABASE products_db;
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a User with Permissions

Next, create a user with admin privileges:

CREATE USER your_username WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE products_db TO your_username;
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Table to Store Products

Finally, create a table that will store products and their attributes in JSONB format:

CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    attributes JSONB
);
Enter fullscreen mode Exit fullscreen mode

This table structure allows you to store dynamic attributes for each product, taking advantage of PostgreSQL's JSONB feature for flexibility and performance.

3. Starting and Stopping PostgreSQL

You can manually start or stop PostgreSQL on macOS using the following commands:

  • Start PostgreSQL:
  brew services start postgresql
Enter fullscreen mode Exit fullscreen mode
  • Stop PostgreSQL:
  brew services stop postgresql
Enter fullscreen mode Exit fullscreen mode
  • Restart PostgreSQL:
  brew services restart postgresql
Enter fullscreen mode Exit fullscreen mode

4. PostgreSQL Uninstallation (Optional)

If you ever need to uninstall PostgreSQL from macOS, you can do so using Homebrew:

brew uninstall postgresql
Enter fullscreen mode Exit fullscreen mode

Conclusion

By following these steps, macOS users can easily install and configure PostgreSQL. With Homebrew, the installation process is quick and seamless.

For a smooth experience, always ensure that PostgreSQL services are running and properly configured before proceeding.

Thanks for reading...
Happy Coding!

Top comments (1)

Collapse
 
dailysandbox profile image
Art

love the tutorial! however, nowadays it seems asking chatGPT is a pretty good way to get the instructions :)