Introduction
PostgreSQL is a powerful open-source relational database management system with robust features for handling large datasets and supporting complex queries. While there are many ways you can install the PostgreSQL database on your laptop directly or using a VM(Virtual machine). However dedicated resources to a VM or server can often slow down your computer. I find it convenient to use Docker, as it gives me the flexibility to quickly start and stop while reducing the load on my computer. This is very helpful when you trying to practice & learn Postgresql. How ever numerous developers use docker for development. You can start and stop PostgreSQL while storing your data persistently.
Docker, a popular containerization platform, allows you to run applications in isolated environments with ease. In this article I will try to give a step-by-step guide and 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.
You must launch the app to have the docker command work from a terminal.
Running PostgreSQL
Here are the steps
Launch a Terminal
We need to start with a terminal session running on our machine. You can do this from MacOS or Windows.
For MacOS, open the Terminal application on your Mac. You will find it by searching for "Terminal" in Spotlight or navigating to Applications -> Utilities -> Terminal. Click the icon to start the terminal.
For Windows, open Windows Menu. You will find the “cmd” prompt or “PowerShell”. Start either one of these,
Pull the PostgreSQL Image
In the Terminal, execute the following command to pull the latest PostgreSQL Docker image from Docker Hub:
art@arvinds-mbp# 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:
art@arvinds-mbp# docker images
Create a Volume for Docker
Now we will create a docker volume so we can store data persistently for the PostgreSQL database. If we do not have a docker volume attached to the docker container, every time we start/stop the container the data will be lost. To create a volume, run the create volume commands for docker, as shown here. The name for the volume is pg-data.
art@arvinds-mbp# docker create volume pg-data
To check if the volume has been created, run
art@arvinds-mbp# docker volume ls
Create a PostgreSQL Container
You will need to create a docker hub account if you don’t have one already at this link. Once the image has been downloaded, create a PostgreSQL container using the below command.
art@arvinds-mbp# docker run postgres -e POSTGRES_PASSWORD=Your-Password -p 5432:5432 -v pg-data:/var/lib/postgresql/data -d postgres Sample Output: art@arvinds-mbp# docker run --name data-engineering-postgres -e POSTGRES_PASSWORD=secret -d postgres 3f0d664d47950f08bc188e9529ce1c0a3715b9cb6fa5ce0d4a05f753513613f1 art@arvinds-mbp#
The commands explanation:
- docker run : Run a docker command
- postgres : name of the container for docker
- -e POSTGRES_PASSWORD : Initialize Postgres with whatevef is typed after the equals (=) sign as the password
- -p 5432:5432 : port_on_host:port_on_container (port mapping). This maps the default postgresql port for your connection.
- -v pg-data:/var/lib/postgresql/data : Map your pg-data volume you created in step 3 to Postgres container data directory /var/lib/postgresql/data
- -d postgres : initialize postgres database running detached from the terminal
Verify and Log Into PostgreSQL
Now that the PostgreSQL has been initiated let us verify if the container is up and running by using the cmd.
art@arvinds-mbp# docker ps
Sample Output : CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a269282b057d postgres "docker-entrypoint.s…" 16 hours ago Up 4 seconds 0.0.0.0:5432->5432/tcp postgres-container
The database is ready to accept connections. Try to login by using the below command
art@arvinds-mbp# docker exec -it postgres psql -U postgres
This will connect you to the PostgreSQL container running on docker through the psql command line.
Start using PostgreSQL
Let's try to create a database and some tables in Postgres
postgres=# create database postgresdb; CREATE DATABASE postgres=# \l List of databases Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges ------------+----------+----------+-----------------+------------+------------+------------+-----------+----------------------- postgresdb | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | postgres | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | template0 | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/postgres + | | | | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/postgres + | | | | | | | | postgres=CTc/postgres (4 rows) postgres=# \c postgresdb You are now connected to database "postgresdb" as user "postgres". postgresdb=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | test | table | postgres (1 row) postgresdb=# insert into test (id,name) values (1,'arvind'); INSERT 0 1 postgresdb=# insert into test (id,name) values (2,'arvind2'); INSERT 0 1 postgresdb=# select * from test; id | name ----+--------- 1 | arvind 2 | arvind2 (2 rows) postgresdb=# exit
Stop the Container and Restart It
Since we don't want to lose all our work, we have already created a dedicated volume to store all our data. We want to check this is working.
Stop the container by using the below command
art@arvinds-mbp# docker stop postgres postgres art@arvinds-mbp#
Check to confirm the container has been stopped
art@arvinds-mbp# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES art@arvinds-mbp#
Now let us start the container and use it
arvindtoorpu@nikithas-mbp ~ arvindtoorpu@nikithas-mbp ~ 0ocker start postgres postgres
Login into PostgreSQL database (container) and check if our data is safe
art@arvinds-mbp# docker exec -it postgres psql -U postgres psql (16.4 (Debian 16.4-1.pgdg120+1)) Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges ------------+----------+----------+-----------------+------------+------------+------------+-----------+----------------------- postgresdb | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | postgres | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | template0 | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/postgres + | | | | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | libc | en_US.utf8 | en_US.utf8 | | | =c/postgres + | | | | | | | | postgres=CTc/postgres (4 rows) postgres=# \c portgresdb You are now connected to database "portgresdb" as user "postgres". portgresdb=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | test | table | postgres (1 row) portgresdb=# select * from test; id | name ----+--------- 1 | arvind 2 | arvind2 (2 rows) portgresdb=#
Reference Links
- Docker docs: https://docs.docker.com/
- Docker hub: https://hub.docker.com/explore
- PostgreSQL: https://www.postgresql.org/
- PostgreSQL Tutorial: https://www.postgresqltutorial.com/postgresql-administration/