1. Overview
1.概述
In this tutorial, we’ll learn to install PostgreSQL with Docker. Generally, we run a Docker container using the public Docker image. Similarly, we can pull pre-configured Docker images of the PostgreSQL database server from Docker Hub. Here, we’ll also demonstrate how PostgreSQL can be installed, configured, and run on Docker.
在本教程中,我们将学习用Docker安装PostgreSQL。一般来说,我们使用公共Docker镜像运行Docker容器。同样地,我们可以从Docker Hub中提取PostgreSQL数据库服务器的预配置Docker镜像。在这里,我们还将演示如何在Docker上安装、配置和运行PostgreSQL。
First, we’ll run a Docker container with a PostgreSQL database using the PostgreSQL Public Image. Later, we’ll create a customized Dockerfile to install the PostgreSQL server in the Docker container. We’ll also learn to backup and restore the database using the Docker container.
首先,我们将使用PostgreSQL公共映像运行一个带有PostgreSQL数据库的Docker容器。随后,我们将创建一个自定义的Docker文件,以便在Docker容器中安装PostgreSQL服务器。我们还将学习如何使用Docker容器备份和恢复数据库。
Let’s deep dive into running a Docker container with the PostgreSQL database.
让我们深入了解一下用PostgreSQL数据库运行Docker容器。
2. Understanding the PostgreSQL Database
2.了解PostgreSQL数据库
Before we move forward to run the Docker container of the PostgreSQL database, let’s first understand the PostgreSQL database. PostgreSQL is an Open-Source RDMS similar to MySQL. It is an object-oriented database, but we can process both structured and unstructured data.
在我们前进到运行PostgreSQL数据库的Docker容器之前,让我们首先了解一下PostgreSQL数据库。PostgreSQL是一个开源的RDMS,与MySQL类似。它是一个面向对象的数据库,但我们可以处理结构化和非结构化的数据。。
The PostgreSQL database engine runs on various platforms, including Windows, Mac OS X, and Linux. It also provides advanced data types and performance optimization features to store and scale complicated database workloads.
PostgreSQL数据库引擎在各种平台上运行,包括Windows、Mac OS X和Linux。它还提供先进的数据类型和性能优化功能来存储和扩展复杂的数据库工作负载。
3. Setup PostgreSQL Using the Public Image
3.使用公共形象设置PostgreSQL
To run a PostgreSQL using Docker, we first need to pull the postgres public image available on Docker Hub:
要使用Docker运行PostgreSQL,我们首先需要在Docker Hub上拉出可用的postgres公共镜像。
$ docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
1fe172e4850f: Pull complete
...
c08147da7b54: Pull complete
Digest: sha256:ab0be6280ada8549f45e6662ab4f00b7f601886fcd55c5976565d4636d87c8b2
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest
In the above command, we pulled the postgres latest stable image. We can also pull a particular version of the postgres image using the below command:
在上面的命令中,我们拉出了postgres最新的稳定镜像。我们也可以用下面的命令拉出一个特定版本的postgres镜像。
$ docker pull postgres:14.2
14.2: Pulling from library/postgres
Digest: sha256:e3d8179786b8f16d066b313f381484a92efb175d1ce8355dc180fee1d5fa70ec
Status: Downloaded newer image for postgres:14.2
docker.io/library/postgres:14.2
Now, we’ll run the Docker container using postgres:latest image using the below command:
现在,我们将使用postgres:latest图像,使用以下命令运行Docker容器。
$ docker run -itd -e POSTGRES_USER=baeldung -e POSTGRES_PASSWORD=baeldung -p 5432:5432 -v /data:/var/lib/postgresql/data --name postgresql postgres
5aeda2b20a708296d22db4451d0ca57e8d23acbfe337be0dc9b526a33b302cf5
The above command uses environment variables POSTGRES_USER and POSTGRES_PASSWORD to set the username and password for the PostgreSQL database. By default, the PostgreSQL database runs on the 5432 port. We exposed the 5432 port on the host using the “-p 5432:5432” in the docker run command.
To back up the data, we also mounted the /var/lib/postgresql/data directory to the /data directory of the host machine of the postgres container.
上述命令使用环境变量POSTGRES_USER和POSTGRES_PASSWORD来设置PostgreSQL数据库的username和password。默认情况下,PostgreSQL数据库运行在5432端口。我们在主机上使用“-p 5432:5432”在docker run命令中暴露了5432端口。
为了备份数据,我们还将/var/lib/postgresql/data目录挂载到postgres容器的主机的/data目录。
psql is a command-line utility used to access PostgreSQL databases interactively. Let’s now use the psql to connect with the database:
psql是一个命令行工具,用于交互式地访问PostgreSQL数据库。现在让我们使用psql来连接数据库。
$ PGPASSWORD=baeldung psql -U baeldung
In order to get the list out of all the databases, we’ll use the command \l :
为了得到所有数据库的列表,我们将使用\l命令。
$ PGPASSWORD=baeldung psql -U baeldung -c '\l'
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+------------+----------+------------+------------+---------------------------
baeldung | baeldung | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | baeldung | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | baeldung | UTF8 | en_US.utf8 | en_US.utf8 | =c/baeldung +
| | | | | baeldung=CTc/baeldung
template1 | baeldung | UTF8 | en_US.utf8 | en_US.utf8 | =c/baeldung +
| | | | | baeldung=CTc/baeldung
(4 rows)
In the above output, we can get the detail of all the databases present on the PostgreSQL server.
在上面的输出中,我们可以得到PostgreSQL服务器上的所有数据库的细节。
4. Setup PostgreSQL Using Customised Dockerfile
4.使用自定义的Docker文件设置PostgreSQL
We can also set up the PostgreSQL database server by creating a customized Dockerfile. Here we’ll create a Dockerfile that will contain all the required commands to install Postgres using CentOS as the base image:
我们也可以通过创建一个自定义的Dockerfile来设置PostgreSQL数据库服务器。在这里,我们将创建一个Dockerfile,它将包含安装Postgres所需的所有命令,使用CentOS作为基础镜像。
FROM centos:7
COPY startUpScript.sh /
RUN yum install -y epel-release maven wget \
&& yum clean all \
&& yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm \
&& yum install -y postgresql11-server postgresql11-contrib \
&& chown root /startUpScript.sh \
&& chgrp root /startUpScript.sh \
&& chmod 777 /startUpScript.sh
CMD ["/bin/bash","-c","/startUpScript.sh && tail -f /dev/null"]
In the above Dockerfile, we used startUpScript.sh to start the PostgreSQL database server on successful installation. Let’s look into the startUpScript.sh file:
在上面的Docker文件中,我们用startUpScript.sh在安装成功后启动PostgreSQL数据库服务器。让我们看看startUpScript.sh文件的内容。
#!/bin/bash
su -l postgres -c /usr/pgsql-11/bin/initdb
su -l postgres -c "/usr/pgsql-11/bin/pg_ctl -D /var/lib/pgsql/11/data -l /tmp/pg_logfile start"
createdb -U postgres baeldung
In the startUpScript.sh, we first initialized the PostgreSQL database and then created a dummy database baeldung.
在startUpScript.sh中,我们首先初始化了PostgreSQL数据库,然后创建了一个虚拟数据库baeldung。
5. Install pgAdmin on Docker
5.在Docker上安装pgAdmin
So far, the PostgreSQL server is active and running on the 5432 port. Now, we’ll install pgAdmin, a web-based user interface tool used to manage PostgreSQL databases and services. pgAdmin can be used to run SQL queries on PostgreSQL databases.
到目前为止,PostgreSQL服务器已经激活并运行在5432端口。现在,我们将安装pgAdmin,这是一个基于Web的用户界面工具,用于管理PostgreSQL数据库和服务。pgAdmin可以用来在PostgreSQL数据库上运行SQL查询。
To perform all the queries from the UI, we can use the pgAdmin, and for that, we need to pull the pgAdmin image using the following command:
为了从用户界面上执行所有的查询,我们可以使用pgAdmin,为此,我们需要使用以下命令拉出pgAdmin图片。
$ docker pull dpage/pgadmin4:latest
latest: Pulling from dpage/pgadmin4
40e059520d19: Pull complete
...
6d23acfae6ef: Pull complete
Digest: sha256:f820e5579857a7210599f998c818777a2f6f39172b50fbeb2faaa1a70413e9ac
Status: Downloaded newer image for dpage/pgadmin4:latest
docker.io/dpage/pgadmin4:latest
To demonstrate, let’s run the container using the below command:
为了演示,让我们使用下面的命令来运行容器。
$ docker run --name pgadmin-baeldung -p 5051:80 -e "PGADMIN_DEFAULT_EMAIL=user@baeldung.com" -e "PGADMIN_DEFAULT_PASSWORD=baeldung" -d dpage/pgadmin4
In the above command, we provided the PGADMIN_DEFAULT_EMAIL and PGADMIN_DEFAULT_PASSWORD as an environment variable to the pgadmin-baeldung container:
在上面的命令中,我们提供了PGADMIN_DEFAULT_EMAIL和PGADMIN_DEFAULT_PASSWORD作为一个环境变量给pgadmin-baeldung容器。
We can easily access PostgreSQL databases using the pgAdmin GUI. In order to access the database, we have to set up a connection to the Postgres server using pgAdmin. We can do this by logging in pgAdmin.
我们可以使用pgAdmin GUI轻松访问PostgreSQL数据库。为了访问数据库,我们必须使用pgAdmin建立一个与Postgres服务器的连接。我们可以通过登录pgAdmin来做到这一点。
6. BackUp and Restore the Data
6.备份和恢复数据
In this section, we’ll learn to backup and restore the data in PostgreSQL using Docker commands.
在本节中,我们将学习使用Docker命令来备份和恢复PostgreSQL中的数据。
First, to back up the data, let’s create a dummy database baeldung and a table baeldungauthor.
首先,为了备份数据,让我们创建一个虚拟数据库baeldung和一个表baeldungauthor.。
$ createdb -h localhost -p 5432 -U baeldung baeldung
The command to create a table is as follows:
创建一个表的命令如下。
CREATE TABLE baeldungauthor (
AUTHOR_ID INT PRIMARY KEY NOT NULL,
AUTHOR_NAME TEXT NOT NULL,
AUTHOR_AGE INT NOT NULL,
AUTHOR_LEVEL INT NOT NULL
);
Let’s list out the created table in the database:
让我们把数据库中创建的表列出来。
psql -U baeldung -d baeldung -c "\d"
List of relations
Schema | Name | Type | Owner
--------+----------------+-------+------------
public | baedlungauthor | table | baeldung
(1 row)
Now use the below command to get the schema detail of table baeldungauthor:
现在使用下面的命令来获取表baeldungauthor的模式细节。
psql -U baeldung -d baeldung -c "\d baedlungauthor"
Table "public.baedlungauthor"
Column | Type | Collation | Nullable | Default
--------------+---------+-----------+----------+---------
author_id | integer | | not null |
author_name | text | | not null |
author_age | integer | | not null |
author_level | integer | | not null |
Indexes:
"baedlungauthor_pkey" PRIMARY KEY, btree (author_id)
Till now, we have created a database and a table. Let’s look into the command to back up a database for a Docker container:
到现在为止,我们已经创建了一个数据库和一个表。让我们来看看为Docker容器备份数据库的命令。
$ docker exec -t postgresql pg_dumpall -c -U baeldung > dump.sql
Here, in the above command, we used the pg_dumpall to backup the baeldung database. It is a standard PostgreSQL tool for backing up the database. In the command, we provided the username of the DB server to access the privileges.
在这里,在上述命令中,我们使用pg_dumpall来备份baeldung数据库。它是一个标准的PostgreSQL工具,用于备份数据库。在命令中,我们提供了DB服务器的用户名来访问权限。
Now let’s check out the command to restore the database:
现在我们来看看恢复数据库的命令。
$ cat dump.sql | docker exec -i postgresql psql -U baeldung
Here, in short, we restored all the tables of the baeldung database using the psql command.
这里,简而言之,我们使用psql命令恢复了baeldung数据库的所有表。
7. Conclusion
7.结语
In this article, we learned to install the PostgreSQL database using the Docker container. We explored each step to pull, set up, and run a Docker container of Postgres. In addition, we explored both the ways to access the PostgreSQL database server. First, we explored the pgAdmin to access the PostgreSQL database server running on the Docker container. Later, we used psql to execute the queries against the databases in PostgreSQL.
在这篇文章中,我们学习了使用Docker容器安装PostgreSQL数据库。我们探讨了拉动、设置和运行Postgres的Docker容器的每个步骤。此外,我们探索了访问PostgreSQL数据库服务器的两种方法。首先,我们探索了pgAdmin来访问运行在Docker容器上的PostgreSQL数据库服务器。后来,我们使用psql来执行对PostgreSQL数据库的查询。
To sum up, we ran the Docker container with the PostgreSQL database using the Postgres public image present on Docker Hub. We also created our customized Dockerfile to install the PostgreSQL server in the Docker container.
总而言之,我们使用Docker Hub上的Postgres公共镜像运行了带有PostgreSQL数据库的Docker容器。我们还创建了自定义的Docker文件,以便在Docker容器中安装PostgreSQL服务器。
Finally, we looked into the backup and restoration of the data in the PostgreSQL database with the Docker container.
最后,我们研究了用Docker容器对PostgreSQL数据库中的数据进行备份和恢复。
