PostgreSQL using Docker

PostgreSQL, often simply “Postgres“, is an object-relational database management system (ORDBMS) with an emphasis on extensibility and standards-compliance. As a database server, its primary function is to store data, securely and supporting best practices, and retrieve it later, as requested by other software applications, be it those on the same computer or those running on another computer across a network (including the Internet). It can handle workloads ranging from small single-machine applications to large Internet-facing applications with many concurrent users. Recent versions also provide replication of the database itself for security and scalability.

Docker has become a standard in the IT industry when it comes to packaging, deploying, and running distributed applications with ease. Docker containers let you quickly spin up new applications without cluttering your system with dependencies.

on this demo we will show you how to run postgres instance inside container, first we will pull oracle linux image, run it on a container, then we will install postgresql manually inside that container.

this demo require docker installed on your server, we use docker version 20.10

pull an Oracle linux image

we want to run postgres on oracle linux base, so we need to pull the image from docker repository

# docker pull oraclelinux:8.4

build a container (this will only create container for a while, after logout from the container, the container status will be exited)

# docker run -it oraclelinux:8.4 /bin/bash

to see all status of container whatever is running or exited

# docker ps -a

to make the container keep running we can use this command

# docker start $(docker ps -a -q --filter "status=exited")

rename container for easy access

# docker container rename 8b5ad665b6d9 postgres13_test1

note: 8b5ad665b6d9 is ID for previous container that we made

connect to postgres13_test1 container

# docker exec -it postgres13_test1 bash

install and configure postgres13 inside container

run this command inside container

# dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# dnf install -y postgresql13 postgresql13-server postgresql13-contrib postgresql13-devel
# su – postgres
$ /usr/pgsql-13/bin/initdb -D /var/lib/pgsql/13/data
$ /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data start
$ psql

if you wan to change some parameter, the configuration file for postgres is in directory /var/lib/pgsql/13/data/postgresql.conf

to archive this container into tarball you can use this command

# docker container export -o postgres13oracle postgres13_test1

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>