How to Install PostgreSQL on macOS with HomeBrew: A Step-by-Step Tutorial
This step-by-step tutorial will guide you through the process of installing PostgreSQL on macOS using Homebrew.
PostgreSQL, often referred to as Postgres, is a robust and open-source relational database management system known for its advanced features and reliability. If you’re a macOS user and want to harness the power of PostgreSQL for your projects, this step-by-step tutorial will guide you through the process of installing PostgreSQL on macOS using Homebrew. Homebrew is a package manager for macOS that simplifies the installation of various software packages, including PostgreSQL.
Preconditions
Before you begin the installation process, ensure you have met the following prerequisites:
-
A macOS system (macOS Catalina or later).
-
Xcode Command Line Tools installed. You can install them by running the following command in your terminal if they are not already installed:
xcode-select --install
-
Homebrew installed. If you haven’t installed Homebrew, visit Homebrew’s website and follow the installation instructions.
With these prerequisites in place, let’s proceed with the installation.
Installing PostgreSQL on macOS with Homebrew
Step 1: Update Homebrew
Ensure Homebrew is up to date by running:
brew update
This command updates the package list and ensures you have the latest version of Homebrew.
Step 2: Install PostgreSQL
To install PostgreSQL with Homebrew, run the following command:
brew install postgresql
This command will download and install PostgreSQL on your macOS system.
Step 3: Start PostgreSQL Service
After installing PostgreSQL, you can start the PostgreSQL service with the following command:
brew services start postgresql
This command not only starts the PostgreSQL service but also sets it to launch automatically at startup.
Step 4: Create a PostgreSQL User and Database
Now that PostgreSQL is up and running, let’s create a new PostgreSQL user and database. Open a terminal and enter the PostgreSQL interactive terminal:
psql
Inside the PostgreSQL interactive terminal, run the following commands to create a user and a database:
CREATE USER your_user WITH PASSWORD 'your_password';
CREATE DATABASE your_database;
GRANT ALL PRIVILEGES ON DATABASE your_database TO your_user;
Replace your_user
, your_password
, and your_database
with your preferred values.
Step 5: Creating a Table
To demonstrate PostgreSQL’s capabilities, let’s create a simple table in the newly created database. Still within the PostgreSQL interactive terminal, execute the following SQL commands:
\c your_database;
CREATE TABLE example (
id serial PRIMARY KEY,
name VARCHAR (100),
age INT
);
Managing the PostgreSQL Service
You can manage the PostgreSQL service on macOS using Homebrew with the following commands:
-
Start PostgreSQL service:
brew services start postgresql
-
Stop PostgreSQL service:
brew services stop postgresql
-
Restart PostgreSQL service:
brew services restart postgresql
Conclusion
Congratulations! You have successfully installed PostgreSQL on your macOS system using Homebrew, created a database, and learned how to manage the PostgreSQL service. PostgreSQL’s extensive feature set makes it a versatile choice for various data storage needs.
With PostgreSQL now installed and operational, you are well-prepared to build and manage databases for your macOS-based applications, leveraging its scalability and reliability for your data-driven projects. Explore PostgreSQL’s documentation to discover its full potential and adapt it to your specific requirements.
If you want to learn more about MySQL, please use our PostgreSQL tutorials and PostgreSQL Reference.