Install PostgreSQL Docker Image

Install PostgreSQL Docker Image

June 2024

This tutorial goes through installing postgresql as a docker image and creating a test user and test database in the postgres docker install. For the official and complete documentation see: https://hub.docker.com/_/postgres

Pull the PostgreSQL Docker Image

Our first step is to pull the official PostgreSQL Docker image, this will install the image with the default build.

							
							
					docker pull postgres:latest				
			

Run the PostgreSQL Docker Image

Next we will run the image declaring the name of the docker process and the Postgres Password.

							
							
					docker run ––name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres				
			

Declaring the Postgres Password Via Docker Secrets Files

Rather than including the Postgres Password in the run script we can include the password in a Docker Secrets file in /run/secrets/secret_name by appending _FILE to the end of POSTGRES_PASSWORD as shown:

							
							
					docker run --name my-postgres -e POSTGRES_PASSWORD_FILE=/run/secrets/postgres-passwd -d postgres				
			

Logging into the Postgres Server

Next we will log into the PostgreSQL server and begin configuring it to our liking.

							
							
					docker exec -it my-postgres psql -U postgres				
			

Creating a Test User

The first process we will run is to create a test user on the server. 

							
							
					CREATE USER username WITH PASSWORD 'password';				
			

To assign this user a role such as SUPERUSER you would enter:

							
							
					ALTER USER username WITH SUPERUSER;				
			

Creating a Test Database

Next we will create a test database and set our new user as the owner of the database.

							
							
					CREATE DATABASE mydatabase OWNER username;				
			

Notes

In keeping with best practices to keep the attack surface as low as possible, when you pull the postgress docker image, it installs the bare necessities for postgres to run. Often when running commands inside the docker image you will find that certain commands do not work as they normally will on a standard Ubuntu server, for instance when editing the Postgres configuration file, you will notice that nano is not installed, I recommend that rather than installing unnecessary utilities, deamons and apps, to simply copy the conf files to the machine docker is running on, and then simply copying them back after editing them on the machine. For instance copy the conf files to your machine via:

							
							
					docker cp <DOCKER_PROCESS_NAME>:/var/lib/postgresql/data/postgresql.conf ./postgresql.conf

docker cp <DOCKER_PROCESS_NAME>:/var/lib/postgresql/data/pg_hba.conf ./pg_hba.conf				
			

Edit the files and then copy them back over into the docker image: 

							
							
					docker cp ./postgresql.conf <DOCKER_PROCESS_NAME>:/var/lib/postgresql/data/postgresql.conf

docker cp ./pg_hba.conf <DOCKER_PROCESS_NAME>:/var/lib/postgresql/data/pg_hba.conf				
			

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.