likes
comments
collection
share

一文带你简单安装docker PostgreSql🕵️

作者站长头像
站长
· 阅读数 14

一文带你简单安装docker PostgreSql🕵️

🧙还在犹豫要不要学习PostgreSql?或者你还在担心PostgreSql如何安装和配置?在本文中,我们将一起学习如何使用docker安装PostgreSql。

我最喜欢使用[docker]的原因就是,我们真的不必太担心构建系统或配置。Docker容器非常轻巧 (消耗更少的资源),非常易于安装和使用。

如果你还是不太了解和熟悉docker,参阅官方文档,在你的笔记本电脑上安装docker,然后按照步骤操作可能会更有帮助。

本文学习目标:

  • 如何拉取Postgres镜像并创建PostgreSQL容器。
  • 如何通过bash连接访问容器
  • 如何使用psql直接连接到数据库。
  • 更改Postgres参数需要什么。

🧙🏻拉取Postgres镜像并创建PostgreSQL容器

创建Postgres容器的第一件事是要有一个容器镜像。让我们验证我们存储库中的所有镜像,执行以下命令:查看docker中所有镜像。

$ docker images
REPOSITORY   TAG           IMAGE ID      CREATED       SIZE
postgres     14-alpine     a762fe0bf572  2 months ago  216MB
postgres     14-alpine3.16 a762fe0bf572  2 months ago  216MB
postgres     14.5-alpine   a762fe0bf572  2 months ago  216MB
pg-dump      13            646d500e76f2  8 months ago  234MB

让我们访问PostgreSQL docker镜像存储库

$ docker pull postgres:13.0
13.0: Pulling from library/postgres
bb79b6b2107f: Already exists 
e3dc51fa2b56: Pull complete 
f213b6f96d81: Pull complete 
2780ac832fde: Pull complete 
ae5cee1a3f12: Pull complete 
95db3c06319e: Pull complete 
475ca72764d5: Pull complete 
8d602872ecae: Pull complete 
c4fca31f2e3d: Pull complete 
a00c442835e0: Pull complete 
2e2305af3390: Pull complete 
6cff852bb872: Pull complete 
25bb0be11543: Pull complete 
4738c099c4ad: Pull complete 
Digest: sha256:8f7c3c9b61d82a4a021da5d9618faf056633e089302a726d619fa467c73609e4
Status: Downloaded newer image for postgres:13.0
docker.io/library/postgres:13.0

再次查看本地镜像:

$ docker images
REPOSITORY   TAG           IMAGE ID      CREATED       SIZE
postgres     13.0      		 c96f8b6bc0d9  2 years ago   314MB
postgres     14-alpine     a762fe0bf572  2 months ago  216MB
postgres     14-alpine3.16 a762fe0bf572  2 months ago  216MB
postgres     14.5-alpine   a762fe0bf572  2 months ago  216MB
pg-dump      13            646d500e76f2  8 months ago  234MB

接下来,让我们创建第一个Postgres容器。

执行以下命令查看所有的docker容器

$ docker ps –all 
CONTAINER ID   IMAGE                    COMMAND     CREATED      STATUS      
be2b3702b8c7   portainer/agent:2.11.1   "./agent"   2 days ago   Up 2 days   

执行以下命令运行docker pg:

$ docker run -it -d –name postgres_db -e POSTGRES_PASSWORD=postgres -d postgres

注意: 要创建容器,使用此命令 docker run -it -d

–name 识别容器的标签

-e POSTGRES_PASSWORD是创建容器时的强制参数。

接下来,让我们使用docker ps命令查看正在运行的容器

$ docker ps
CONTAINER ID   IMAGE                    COMMAND     							CREATED      				STATUS      
be2b3702b8c7   portainer/agent:2.11.1   "./agent"   							2 days ago   				Up 2 days   
67a4705c263c   postgres:13               "docker-entrypoint.s…"   4 months ago        Up 2 days           0.0.0.0:5432->5432/tcp

🧙🏻如何通过bash连接访问容器

让我们使用以下命令使用bash连接到容器。将会看到有一些Postgres进程在后台运行 (checkpointer、walwriter、stats collector等等)

docker exec -it 67a4705c263c /bin/bash
root@67a4705c263c:/# ps -ef | grep postgres
postgres     1     0  0 Oct30 ?        00:00:02 postgres
postgres    26     1  0 Oct30 ?        00:00:00 postgres: checkpointer  
postgres    27     1  0 Oct30 ?        00:00:01 postgres: background writer  
postgres    28     1  0 Oct30 ?        00:00:01 postgres: walwriter  
postgres    29     1  0 Oct30 ?        00:00:01 postgres: autovacuum launcher  
postgres    30     1  0 Oct30 ?        00:00:03 postgres: stats collector  
postgres    31     1  0 Oct30 ?        00:00:00 postgres: logical replication launcher  
root      3505  3498  0 01:05 pts/0    00:00:00 grep postgres

🧙如何使用psql直接连接到数据库

现在,让我们执行psql连接到数据库。

root@67a4705c263c:/# psql -U postgres

连接到数据库的另一种方法是在连接到Postgres容器本身时使用psql。

$ docker exec -it 67a4705c263c psql -U postgres

让我们使用以下查询命令:

postgres=# select now();
             now
-----------------------------
2022-11-02 01:09:40.608002+00
(1 row)

🧙更改Postgres参数需要什么

接下来,让我们尝试将参数shared_buffers从默认值128MB更改为10mb。由于此参数需要重新启动postgres服务,因此我们必须确保这样做才能实现。

postgres=# alter system set shared_buffers=10 MB;

重启服务,查看shared_buffers 参数

postgres=# show shared_buffers;
shared_buffers
--------------
    128MB
(1 row)

重新启动Postgres容器。如果你想从容器内部重新启动postgres服务,它将崩溃并停止容器。请记住,容器本身就是postgres服务!

$ docker restart 67a4705c263c

让我们链接pg查看刚才修改参数配置的结果

$ docker exec -it 67a4705c263c psql -U postgres
postgres=# show shared_buffers;
shared_buffers
--------------
    10MB
(1 row)