docker 安装postgresql笔记

it2024-12-08  9

1、查找镜像

[root@k8s ~]# docker search postgres NAME DESCRIPTION STARS OFFICIAL AUTOMATED postgres The PostgreSQL object-relational database sy… 8474 [OK] sameersbn/postgresql 155 [OK] paintedfox/postgresql A docker image for running Postgresql. 77 [OK] bitnami/postgresql Bitnami PostgreSQL Docker Image 64 [OK] centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 46 postgrest/postgrest REST API for any Postgres database 34 arm32v7/postgres The PostgreSQL object-relational database sy… 25 wrouesnel/postgres_exporter Postgres metrics exporter for Prometheus. 24 circleci/postgres The PostgreSQL object-relational database sy… 23 centos/postgresql-10-centos7 PostgreSQL is an advanced Object-Relational … 18 centos/postgresql-94-centos7 PostgreSQL is an advanced Object-Relational … 16 schickling/postgres-backup-s3 Backup PostgresSQL to S3 (supports periodic … 16 [OK] debezium/postgres PostgreSQL for use with Debezium change data… 15 [OK] postdock/postgres PostgreSQL server image, can work in master … 13 [OK] prodrigestivill/postgres-backup-local Backup PostgresSQL to local filesystem with … 12 [OK] clkao/postgres-plv8 Docker image for running PLV8 1.4 on Postgre… 12 [OK] camptocamp/postgres Docker image for PostgreSQL including some e… 7 [OK] centos/postgresql-95-centos7 PostgreSQL is an advanced Object-Relational … 6 jgiannuzzi/postgres-bdr Docker image for PostgreSQL with BDR support 5 [OK] blacklabelops/postgres Postgres Image for Atlassian Applications 4 [OK] dcm4che/postgres-dcm4chee PostgreSQL for dcm4che-arc 5.x 3 [OK] ansibleplaybookbundle/postgresql-apb An APB which deploys RHSCL PostgreSQL 2 [OK] fredboat/postgres PostgreSQL 10.0 used in FredBoat's docker-co… 1 manageiq/postgresql Container with PostgreSQL and built on CentO… 0 [OK] openshift/postgresql-92-centos7 DEPRECATED: A Centos7 based PostgreSQL v9.2 … 0 [root@k8s ~]#

2、下载镜像

[root@k8s ~]# docker pull postgres (默认下载最新的版本,docker pull postgres:11.7 下载指定的版本) Using default tag: latest latest: 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:latest docker.io/library/postgres:latest [root@k8s ~]#

3、查看镜像

[root@k8s ~]# docker images postgres REPOSITORY TAG IMAGE ID CREATED SIZE postgres latest c96f8b6bc0d9 7 days ago 314MB [root@k8s ~]#

4、通过镜像创建容器并运行

[root@k8s ~]# docker run --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "en_US.utf8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". Data page checksums are disabled. fixing permissions on existing directory /var/lib/postgresql/data ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... posix selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default time zone ... Etc/UTC creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. ok Success. You can now start the database server using: pg_ctl -D /var/lib/postgresql/data -l logfile start waiting for server to start....2020-10-21 08:32:39.429 UTC [46] LOG: starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit 2020-10-21 08:32:39.429 UTC [46] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2020-10-21 08:32:39.434 UTC [47] LOG: database system was shut down at 2020-10-21 08:32:39 UTC 2020-10-21 08:32:39.440 UTC [46] LOG: database system is ready to accept connections done server started /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* 2020-10-21 08:32:39.517 UTC [46] LOG: received fast shutdown request waiting for server to shut down....2020-10-21 08:32:39.520 UTC [46] LOG: aborting any active transactions 2020-10-21 08:32:39.527 UTC [46] LOG: background worker "logical replication launcher" (PID 53) exited with exit code 1 2020-10-21 08:32:39.529 UTC [48] LOG: shutting down 2020-10-21 08:32:39.550 UTC [46] LOG: database system is shut down done server stopped PostgreSQL init process complete; ready for start up. 2020-10-21 08:32:39.647 UTC [1] LOG: starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit 2020-10-21 08:32:39.647 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 2020-10-21 08:32:39.647 UTC [1] LOG: listening on IPv6 address "::", port 5432 2020-10-21 08:32:39.652 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2020-10-21 08:32:39.657 UTC [55] LOG: database system was shut down at 2020-10-21 08:32:39 UTC 2020-10-21 08:32:39.662 UTC [1] LOG: database system is ready to accept connections

5、删除容器,创建主机映射postgres的data目录

[root@k8s ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e63b736f0f3f postgres "docker-entrypoint.s…" 2 minutes ago Exited (0) About a minute ago postgres a13db31b871c mysql "docker-entrypoint.s…" About an hour ago Up About an hour 33060/tcp, 0.0.0.0:3307->3306/tcp mysqlserver1 f56ed36626a3 mysql "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp, 33060/tcp mysqlserver 3744fe5331d5 hello-world "/hello" 5 hours ago Exited (0) 2 hours ago agitated_meninsky [root@k8s ~]# docker rm e63b736f0f3f e63b736f0f3f [root@k8s ~]# [root@k8s ~]# mkdir -p pgdata [root@k8s ~]# cd pgdata/ [root@k8s pgdata]# [root@k8s pgdata]# docker run --name postgres -v $PWD/data/:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres -d -i -p 5432:5432 postgres aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f [root@k8s pgdata]# [root@k8s pgdata]# ls data [root@k8s pgdata]# cd data [root@k8s data]# ls base pg_dynshmem pg_logical pg_replslot pg_stat pg_tblspc pg_wal postgresql.conf global pg_hba.conf pg_multixact pg_serial pg_stat_tmp pg_twophase pg_xact postmaster.opts pg_commit_ts pg_ident.conf pg_notify pg_snapshots pg_subtrans PG_VERSION postgresql.auto.conf postmaster.pid [root@k8s data]#

run,创建并运行一个容器; --name,指定创建的容器的名字; -e POSTGRES_PASSWORD=password,设置环境变量,指定数据库的登录口令为password; -p 5432:5432,端口映射将容器的5432端口映射到外部机器的54321端口; 

6、检查postgres容器的状态

[root@k8s data]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES aec8a31d2ae1 postgres "docker-entrypoint.s…" 25 seconds ago Up 24 seconds 0.0.0.0:5432->5432/tcp postgre

7、进入postgres容器

[root@k8s data]# docker exec -it postgres /bin/bash root@aec8a31d2ae1:/# ls bin boot dev docker-entrypoint-initdb.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@aec8a31d2ae1:/# psql psql: error: could not connect to server: FATAL: role "root" does not exist root@aec8a31d2ae1:/# su - postgres postgres@aec8a31d2ae1:~$ postgres@aec8a31d2ae1:~$ postgres@aec8a31d2ae1:~$ psql psql (13.0 (Debian 13.0-1.pgdg100+1)) Type "help" for help. postgres=# postgres=# show port; port ------ 5432 (1 row) postgres=# show config_file ; config_file ------------------------------------------ /var/lib/postgresql/data/postgresql.conf (1 row) postgres=# \q postgres@aec8a31d2ae1:~$

8、容器外连接测试

没有安装psql客户端 [root@k8s bin]# psql bash: psql: command not found... [root@k8s bin]# [root@k8s bin]# 安装postgressql,主要为了使用psql工具 [root@k8s ~]# yum install postgresql Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package postgresql.x86_64 0:9.2.24-4.el7_8 will be installed --> Processing Dependency: postgresql-libs(x86-64) = 9.2.24-4.el7_8 for package: postgresql-9.2.24-4.el7_8.x86_64 --> Processing Dependency: libpq.so.5()(64bit) for package: postgresql-9.2.24-4.el7_8.x86_64 --> Running transaction check ---> Package postgresql-libs.x86_64 0:9.2.24-4.el7_8 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================ Package Arch Version Repository Size ============================================================================================================================================================ Installing: postgresql x86_64 9.2.24-4.el7_8 updates 3.0 M Installing for dependencies: postgresql-libs x86_64 9.2.24-4.el7_8 updates 234 k Transaction Summary ============================================================================================================================================================ Install 1 Package (+1 Dependent package) Total download size: 3.3 M Installed size: 17 M Is this ok [y/d/N]: y Downloading packages: (1/2): postgresql-9.2.24-4.el7_8.x86_64.rpm | 3.0 MB 00:00:00 (2/2): postgresql-libs-9.2.24-4.el7_8.x86_64.rpm | 234 kB 00:00:03 ------------------------------------------------------------------------------------------------------------------------------------------------------------ Total 939 kB/s | 3.3 MB 00:00:03 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : postgresql-libs-9.2.24-4.el7_8.x86_64 1/2 Installing : postgresql-9.2.24-4.el7_8.x86_64 2/2 Verifying : postgresql-9.2.24-4.el7_8.x86_64 1/2 Verifying : postgresql-libs-9.2.24-4.el7_8.x86_64 2/2 Installed: postgresql.x86_64 0:9.2.24-4.el7_8 Dependency Installed: postgresql-libs.x86_64 0:9.2.24-4.el7_8 Complete! [root@k8s ~]# [root@k8s ~]# [root@k8s ~]# psql <=====psql工具已经存在 psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? [root@k8s ~]#

连接测试成功

[root@k8s ~]# psql -h 192.168.40.105 -p 5432 -d postgres -U postgres Password for user postgres: psql (9.2.24, server 13.0 (Debian 13.0-1.pgdg100+1)) WARNING: psql version 9.2, server version 13.0. <=====客户端版本为9.x,服务器版本为13.0 Some psql features might not work. Type "help" for help. postgres=#

9、其他链接方式

其实容器也有自己的IP地址,在/var/lib/docker/containers/容器ID/hosts

[root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# pwd /var/lib/docker/containers/aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# ls -ltr total 32 drwx------ 2 root root 6 Oct 21 16:40 checkpoints -rw-r--r-- 1 root root 174 Oct 21 16:40 hosts -rw-r--r-- 1 root root 71 Oct 21 16:40 resolv.conf.hash -rw-r--r-- 1 root root 85 Oct 21 16:40 resolv.conf drwx------ 2 root root 6 Oct 21 16:40 mounts -rw-r--r-- 1 root root 13 Oct 21 16:40 hostname -rw-r--r-- 1 root root 1557 Oct 21 16:40 hostconfig.json -rw------- 1 root root 3144 Oct 21 16:40 config.v2.json -rw-r----- 1 root root 7531 Oct 21 17:05 aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f-json.log [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# cat hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.4 aec8a31d2ae1 [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]#

连接测试

[root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# pwd /var/lib/docker/containers/aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# ls -ltr total 32 drwx------ 2 root root 6 Oct 21 16:40 checkpoints -rw-r--r-- 1 root root 174 Oct 21 16:40 hosts -rw-r--r-- 1 root root 71 Oct 21 16:40 resolv.conf.hash -rw-r--r-- 1 root root 85 Oct 21 16:40 resolv.conf drwx------ 2 root root 6 Oct 21 16:40 mounts -rw-r--r-- 1 root root 13 Oct 21 16:40 hostname -rw-r--r-- 1 root root 1557 Oct 21 16:40 hostconfig.json -rw------- 1 root root 3144 Oct 21 16:40 config.v2.json -rw-r----- 1 root root 7531 Oct 21 17:05 aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f-json.log [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# cat hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.4 aec8a31d2ae1 [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]# [root@k8s aec8a31d2ae1ca79c63cf64a1e6210f11982807183217da5ed7e657053d5564f]#

 

最新回复(0)