Learn how to run a Postgres Docker container with our step-by-step guide. Simplify your database management with Docker’s ease of use and portability. #centlinux #postgres #docker
PostgreSQL, often referred to as Postgres, is a powerful and versatile open-source relational database management system (RDBMS). It was developed with a focus on extensibility and standards compliance.
PostgreSQL has a strong, active community that contributes to its continuous improvement and provides extensive documentation and support. It also has a variety of third-party tools and extensions that enhance its capabilities, ensuring it remains at the forefront of database technology.
If you are new to Docker platform, then you should read Docker in Action (PAID LINK) by Manning Publications before moving forward with this article.
Running PostgreSQL in a Docker container has several advantages and disadvantages. Here are the key pros and cons:
In summary, running PostgreSQL in a Docker container offers significant benefits in terms of isolation, portability, and management but requires careful consideration of data persistence, performance, and potential complexity in networking and debugging.
Read Also: How to install PostgreSQL on Ubuntu Server 18
Recommended Online Training: PostgreSQL Database Administration on Windows/Linux
We are using a Ubuntu Server based Docker Host with following specification.
We are using a pre-configured Docker host in this article. For setting up the required environment on Red Hat based Linux server, please follow our previous article to install Docker on CentOS.
Connect with docker-01.centlinux.com as a privileged user by using a ssh tool like PuTTY.
Create a directory to store configuration and data files related to PostgreSQL Docker container.
$ mkdir ~/postgres-01 $ cd postgres-01
Create a directory for PostgreSQL data files.
$ mkdir postgres_data
Create a docker-compose.yml file.
$ vi docker-compose.yml
And define postgres service therein.
version: "3.1" services: db: image: "postgres:11" container_name: "postgres-01.centlinux.com" ports: - "5432:5432" environment: POSTGRES_PASSWORD: "123" volumes: - ./postgres_data:/var/lib/postgresql/data links: - "pgadmin"
Pull the required postgres image from Docker Hub.
$ sudo docker image pull postgres:11 11: Pulling from library/postgres ... Status: Downloaded newer image for postgres:11
Starting postgres container using our docker-compose.yml file.
$ sudo docker-compose up [sudo] password for ahmer: Creating postgres-01.centlinux.com ... done Attaching to postgres-01.centlinux.com ... postgres-01.centlinux.com | 2020-03-04 18:48:31.371 UTC [1] LOG: database system is ready to accept connections
Open a new ssh session and connect with postgres container.
$ sudo docker exec -it postgres-01.centlinux.com bash root@7bb0d4f1e4a6:/# su - postgres postgres@7bb0d4f1e4a6:~$ psql psql (11.7 (Debian 11.7-2.pgdg90+1)) Type "help" for help. postgres=# exit postgres@7bb0d4f1e4a6:~$ exit logout root@7bb0d4f1e4a6:/# exit exit
PostgreSQL docker container is successfully configured.
Pull the pgAdmin4 Docker image from Docker Hub.
$ sudo docker pull dpage/pgadmin4 Using default tag: latest latest: Pulling from dpage/pgadmin4 ... Status: Downloaded newer image for dpage/pgadmin4:latest
Edit docker-compose.yml file and add pgadmin service.
$ vi docker-compose.yml
Now define pgadmin service under the services section.
pgadmin: image: "dpage/pgadmin4" container_name: "pgadmin4.centlinux.com" ports: - "5050:80" environment: PGADMIN_DEFAULT_EMAIL: "ahmer@centlinux.com" PGADMIN_DEFAULT_PASSWORD: "123"
Allow the pgAdmin service port in Ubuntu firewall on Docker Host.
$ sudo ufw allow 5050/tcp [sudo] password for ahmer: Rule added Rule added (v6)
Start PostgreSQL and pgAdmin4 Docker containers using docker-compose command.
$ sudo docker-compose up Starting pgadmin4.centlinux.com ... done Starting postgres-01.centlinux.com ... done Attaching to pgadmin4.centlinux.com, postgres-01.centlinux.com ... pgadmin4.centlinux.com | [2020-03-05 14:56:19 +0000] [81] [INFO] Booting worker with pid: 81
Open URL http://docker-01.centlinux.com:5050 in a web browser.
Login as
Click on Add New Server to add a PostgreSQL database server.
Provide connection settings as we have provided above and click on Save.
Our PostgreSQL database server has been added in pgAdmin4 docker container.
Running PostgreSQL in a Docker container is a powerful approach that combines the robustness of PostgreSQL with the flexibility and convenience of Docker. It offers numerous benefits, including consistent environments, simplified management, and scalability. However, it also comes with challenges like ensuring data persistence and handling potential performance overhead. By understanding both the advantages and the potential drawbacks, you can effectively utilize Docker containers to manage your PostgreSQL databases.
If you’re looking for expert guidance on how to run a PostgreSQL Docker container, I offer professional services to help you set up, configure, and optimize your PostgreSQL environment. Check out my Fiverr profile for more details on my services and how I can assist you in achieving a seamless and efficient PostgreSQL setup.
Learn how to change Apache document root in Linux by following this step-by-step guide. Adjust…
Discover how to change Apache port in Linux easily. Follow our simple guide to modify…
Learn how to create a virtual host in Apache Server with this comprehensive guide. Set…
Discover 10 practical tasks for the RHCSA exam with step-by-step solutions. Boost your Linux skills…
Discover the ultimate Fail2ban configuration guide. Learn how to set up, customize, and optimize Fail2ban…
Explore the ultimate guide to VPS servers and learn everything about Virtual Private Servers, including…
This website uses cookies.