Logo AppDev24 Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Databases

Install & Configure PostgreSQL on Mac

Updated on Jun 22, 2024

As a developer or data enthusiast, you may have heard of PostgreSQL, a powerful open-source relational database management system. While it's widely used in many industries, setting up and configuring PostgreSQL can be intimidating for those new to the technology. In this article, we'll walk through the process of installing and configuring PostgreSQL on your Mac using Homebrew

Installing PostgreSQL

The first step is to install PostgreSQL using Homebrew, a popular package manager for macOS. Open terminal and run the following command:

brew install postgresql

This will download and install PostgreSQL on our system.

Starting the PostgreSQL Service

Once installed, we need to start the PostgreSQL service. Run the following command:

brew services start postgresql

This will start the PostgreSQL service, allowing you to interact with it using the psql command-line tool.

Configuring the Database

Now that the service is running, let's create a new user and database. Open terminal and run the following commands:

psql postgres

CREATE USER demouser WITH LOGIN PASSWORD 'P@ssword1234';
ALTER USER demouser CREATEROLE CREATEDB;

\du

These commands will create a new user named demouser with a password of P@ssword1234. The \du command is used to display the list of users in the PostgreSQL database.

Creating a New Database

Next, let's create a new database:

CREATE DATABASE demo;
GRANT ALL PRIVILEGES ON DATABASE demo TO demouser;

\l

\q

The CREATE DATABASE command creates a new database named demo, and the GRANT command grants all privileges on that database to the demouser. The \l command is used to display the list of databases in the PostgreSQL database.

Testing the Database

Finally, let's test our new database:

psql demo -U demouser -W

CREATE SCHEMA sales;

\dn

SET SCHEMA 'sales';
CREATE TABLE dept (deptno NUMERIC PRIMARY KEY, dname VARCHAR(30));

\dt

\q

These commands will connect to the demo database using the demouser account, create a new schema named sales, and then create a table named dept. The \dn command is used to display the list of schemas in the database.

In this article, we've walked through the process of installing and configuring PostgreSQL on our Mac using Homebrew. With these steps, we should now have a fully functional PostgreSQL installation, complete with a new user, database, schema, and table. Whether you're a developer or just starting out with PostgreSQL, this guide should provide a basic setup for getting started with this powerful database management system.

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles