Skip to main content

How to Install PostgreSQL on a Mac using Docker

In this article, I'm going to show you how to install PostgreSQL on a Mac using Docker.

PostgreSQL is a powerful open-source relational database management system with robust features for handling large datasets and supporting complex queries.

Docker, a popular containerization platform, allows you to run applications in isolated environments with ease. In this step-by-step guide, we will walk you through the process of installing PostgreSQL on a Mac using Docker.

Prerequisites

Before you begin, ensure that you have Docker installed on your Mac. You can download and install Docker Desktop from the official Docker website (https://www.docker.com/products/docker-desktop).

You must launch the app to have the docker command work from a terminal.

Step 1. Launch terminal

Open the Terminal application on your Mac. You can find it by searching for "Terminal" in Spotlight or navigating to Applications -> Utilities -> Terminal.

Step 2. Pull the PostgreSQL image

In the Terminal, execute the following command to pull the latest PostgreSQL Docker image from Docker Hub:

docker pull postgres

This command downloads the official PostgreSQL image to your local machine.

To verify that it was pulled down successfully, run this command:

docker images

Step 3. Create a Docker volume

We will create a Docker volume to ensure the persistence of PostgreSQL data. Execute the following command in the Terminal:

docker volume create postgres-data

To verify that the volume was created, run this command:

docker volume ls

Step 4. Create a PostgreSQL container

Once the image is downloaded, create a PostgreSQL container using the following command. Replace <password> with your desired password for the PostgreSQL database.:

docker run --name postgres-container -e POSTGRES_PASSWORD=<password> -p 5432:5432 -v postgres-data:/var/lib/postgresql/data -d postgres

This command creates a Docker container named "postgres-container" with the specified password, exposes port 5432 (the default port for PostgreSQL), and mounts the "postgres-data" volume to the container's data directory.

Step 5. Verify the PostgreSQL container exists

To check if the container is running, execute this command:

docker ps

This command will display a list of running containers. Look for the "postgres-container" in the list.

Step 6. Access PostgreSQL

You can now access PostgreSQL using a PostgreSQL client application or through the command line. To connect using the psql command-line tool, execute the following command:

 docker exec -it postgres-container psql -U postgres

This command connects to the PostgreSQL database running in the container and opens the psql command-line interface.

Step 7. Interact with PostgreSQL

Once connected to the psql command-line interface, you can execute SQL commands and interact with the PostgreSQL database. For example, you can create a new database like this (do not forget the semicolon at the end!):

CREATE DATABASE mydatabase;

To verify that was created, run this command inside psql:

\list

You can also create tables, insert data, perform queries, and manage your PostgreSQL database as needed.

To exit psql, run this command:

quit

Cleanup

If you don't mind losing anything you've setup, you can remove the container, image, data, etc. with the following commands:

To remove the container:

docker stop postgres-container
docker rm postgres-container

To remove the image:

docker rmi postgres

To remove the volume

tip

Only do this if you don't mind deleting all of your databases, etc.

docker volume rm postgres-data

Conclusion

Congratulations! You have installed PostgreSQL on your Mac using Docker. By utilizing a Docker volume, your PostgreSQL data will be stored persistently even if the container is stopped or restarted. This allows for data durability and easier management of your database. Remember to stop and remove the container when you're done to conserve system resources.

info

Some hosting services, like DigitalOcean, offer mulitple ways to host PostgreSQL in the cloud.

See my affiliate link badge in the footer for more info (sometimes they offer a credit).

References

  • docker.com/products/docker-desktop/ - [1]
  • postgresql.org/ - [2]
  • pgAdmin - GUI for PostgreSQL - [3]
  • DBeaver - another GUI for PostgreSQL and several other database types - [4]
  • psql command reference - [5]