Installing PostgreSQL using Docker
source link: https://www.percona.com/blog/installing-postgresql-using-docker/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Following the series of blogs started by Peter Zaitsev in Installing MySQL with Docker, on deploying Docker containers running open source databases, in this article, I’ll demonstrate how to install PostgreSQL using Docker.
Before proceeding, it is important to remind you of Peter’s warning from his article, which applies here as well:
“The following instructions are designed to get a test instance running quickly and easily; you do not want to use these for production deployments.”
We intend to show how to bring up PostgreSQL instance(s) into Docker containers. Moreover, you will find some basic Docker management operations and some snippets containing examples, always based on the Docker Hub PostgreSQL official image.
Installing
Starting with the installation, the following example shows how to bring up a Docker container within the latest PostgreSQL release from Docker Hub. It is required to provide the password of the Postgres user at the moment you are launching the container; otherwise, the container will not be created:
Once you start the container, you can check if it successfully started with docker inspect:
If not, you can try starting it manually (you will see how to do it on the Managing the PostgreSQL Containers section).
Getting Connected to the Containerized Instance
The snippet below will show you how to connect to your PostgreSQL containerized instance.
- Directly from docker:
- Or from a PostgreSQL client connecting to its IP address, which you can obtain with the command docker inspect:
Managing the PostgreSQL Containers
The command below will show you how to list all active containers and their respective statuses:
To stop one of the containers, do:
To start a container:
Finally, to remove a container, first stop it and then:
Customizing PostgreSQL settings in the container
Let’s say you want to define different parameters for the PostgreSQL server running on Docker. You can create a custom postgresql.conf configuration file to replace the default one used with Docker:
Another example of how to pass startup arguments is changing the directory which stores the wal files:
Running Different PostgreSQL Versions in Different Containers
You can launch multiple PostgreSQL containers, each running a different PostgreSQL version. In the example below, you will find how to start up a 10.5 PostgreSQL version:
Here is how you could launch a PostgreSQL 11 container:
After that, by reissuing the status command, you will see all of the already created ones:
You will notice that both 10.5 and 11 were created with different port mappings. Locally into the container, both are listening on 5432 but are externally mapped to different ports. Therefore, you will be able to access all of them externally.
You will find even more details on the PostgreSQL Hub Docker site.
Percona Distribution for PostgreSQL provides the best and most critical enterprise components from the open-source community in a single distribution, designed and tested to work together.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK