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
Table of Contents
What is Postgres?
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.
Key Features of PostgreSQL
- ACID Compliance: Ensures reliability with Atomicity, Consistency, Isolation, and Durability properties.
- Support for Advanced Data Types: Includes support for JSON, XML, and hstore (key-value pairs).
- Full-Text Search: Allows efficient searching of text data.
- Custom Functions and Triggers: Enables users to define their own functions, which can be written in various programming languages, such as PL/pgSQL, PL/Python, and PL/Perl.
- MVCC (Multiversion Concurrency Control): Allows multiple transactions to access the database concurrently without locking, enhancing performance.
- Scalability: Suitable for both small applications and large-scale enterprise solutions, supporting a variety of workloads.
- Extensibility: Users can add new types, operators, index types, and procedural languages.
- Replication and High Availability: Supports synchronous and asynchronous replication for high availability and disaster recovery.
- Robust Security Features: Includes SSL for secure connections, various authentication methods, and row-level security.
Use Cases
- Web Development: Frequently used as a backend database for web applications.
- Data Warehousing: Capable of handling large amounts of data for analytical processing.
- Geospatial Data Management: Supports the PostGIS extension for geographic objects, making it a popular choice for geographic information systems (GIS).
- Enterprise Applications: Trusted by many large organizations for its reliability and performance.
Community and Support
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.
Pros and Cons of Running Postgres Docker Container
Running PostgreSQL in a Docker container has several advantages and disadvantages. Here are the key pros and cons:
Pros
- Isolation: Containers provide a high level of isolation, ensuring that PostgreSQL runs in its own environment without interference from other applications.
- Portability: Docker containers can run on any system that supports Docker, making it easy to move PostgreSQL instances between development, testing, and production environments.
- Consistency: Ensures a consistent environment across different stages of development and deployment, reducing the “it works on my machine” problem.
- Simplified Management: Docker makes it easy to start, stop, and manage PostgreSQL instances with simple commands.
- Scalability: Containers can be quickly scaled up or down to meet changing demands.
- Resource Efficiency: Containers share the host system’s kernel and resources, leading to efficient utilization of system resources.
- Automation: Integration with CI/CD pipelines can streamline database deployment and testing processes.
Cons
- Performance Overhead: Although minimal, there is some performance overhead compared to running PostgreSQL directly on the host system.
- Data Persistence: Proper configuration is needed to ensure data persists across container restarts and updates. This typically involves using Docker volumes or external storage solutions.
- Networking Complexity: Container networking can add complexity, especially in multi-container setups where PostgreSQL needs to communicate with other services.
- Learning Curve: There may be a learning curve for those unfamiliar with Docker and containerization concepts.
- Resource Limits: Containers share the host system’s resources, which can lead to contention if not managed properly.
- Debugging Challenges: Debugging issues within a container can be more complex compared to a traditional setup.
- Compatibility: Some extensions or tools may not work seamlessly in a containerized environment.
Read Also: How to install PostgreSQL on Ubuntu Server 18
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.
Recommended Training: SQL and PostgreSQL for Beginners: Become a SQL Expert from Jon Avis
Docker Host Specification
We are utilizing an Ubuntu Server-based Docker host configured with the following specifications to ensure optimal performance and stability.
- CPU – 3.4 Ghz (2 cores)
- Memory – 2 GB
- Storage – 20 GB
- Operating System – Ubuntu Server 18.04 LTS
- Hostname – docker-01.centlinux.com
- IP Address – 192.168.116.218 /24
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.
How to Run Postgres Docker Container?
Connect to docker-01.centlinux.com as a privileged user using an SSH tool such as PuTTY, Terminal, or any preferred SSH client.
Once connected, create a dedicated directory on the server to store configuration files and data related to the PostgreSQL Docker container. This ensures proper organization, easy management, and data persistence for your PostgreSQL instance.
$ mkdir ~/postgres-01
$ cd postgres-01
Create a dedicated directory to store PostgreSQL data files, ensuring data persistence and organization. This directory will serve as the storage location for database files, allowing PostgreSQL to retain data even if the Docker container is restarted or removed. Proper directory management also helps with backup, security, and easy access to database files.
$ mkdir postgres_data
Create a docker-compose.yml file to define and manage your PostgreSQL container configuration. This file allows you to specify essential settings such as the PostgreSQL image, container name, environment variables, volume mappings for data persistence, and network configurations. Using Docker Compose simplifies container deployment, making it easier to start, stop, and manage multiple containers with a single command.
$ vi docker-compose.yml
Next, define the PostgreSQL service within the docker-compose.yml
file. This configuration should include essential parameters such as the PostgreSQL image version, container name, environment variables (e.g., database name, username, and password), port mapping, and volume bindings to ensure data persistence. Properly defining the service in Docker Compose allows for easy deployment, scalability, and efficient management of your PostgreSQL container.
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"
Download the necessary PostgreSQL Docker image from Docker Hub to ensure you have the latest stable or specified version. This image contains the complete PostgreSQL database system, pre-configured for containerized environments. Pulling the image ensures that all required dependencies are included, allowing for seamless deployment and compatibility with your Docker Compose setup. Make sure to choose the appropriate version based on your application requirements and compatibility needs.
$ sudo docker image pull postgres:11
11: Pulling from library/postgres
...
Status: Downloaded newer image for postgres:11
Starting a 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.
How to Run pgAdmin4 Docker Container?
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
Test PostgreSQL and pgAdmin Containers
Open URL http://docker-01.centlinux.com:5050 in a web browser.
Login as
- User: ahmer@centlinux.com
- Password: 123
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.
Final Thoughts
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.
Struggling with AWS or Linux server issues? I specialize in configuration, troubleshooting, and security to keep your systems performing at their best. Check out my Fiverr profile for details.
FAQs
1. Do I need to install PostgreSQL before running a Postgres Docker container?
No, Docker pulls the required PostgreSQL image automatically when you run the container.
2. How can I persist PostgreSQL data in a Docker container?
You can use Docker volumes or bind mounts to store database data outside the container.
3. Can I connect to a Postgres Docker container from my local machine?
Yes, you need to expose the correct port and use a database client or psql
to connect.
4. How do I restart a stopped Postgres Docker container?
You can restart the container using Docker commands without losing persistent data.
5. Is it safe to use a Postgres Docker container in production?
Yes, but it’s recommended to configure persistent storage, backups, and security settings properly.