使用 Docker 运行 PostgreSQL 数据库

目前发现很多开源项目都使用 postgresql 来存放数据,因此我认为很有必要学习看看。

一开始我使用 Rock 5B 本地安装 postgresql,安装的是 14 的版本。之后要修改配置文件,才能够外部访问。感觉还有挺多需要折腾的。

后来我觉得,还是用 Docker 运行一个来学习好了,快捷方便。而且用完直接删除就行。

我们可以到 Docker Hub 查看 Docker 镜像的情况。我拉取了 4 个镜像,看来用 alpine 标签的镜像最省空间。

# docker images postgres
REPOSITORY TAG IMAGE ID CREATED SIZE
postgres 15 ceccf204404e 12 days ago 379MB
postgres 15.0 027eba2e8939 6 months ago 377MB
postgres alpine 1149d285a5f5 15 months ago 209MB
postgres latest 07e2ee723e2d 15 months ago 374MB

启动

docker run -d --name=postgres-test -p 2345:5432 -e POSTGRES_PASSWORD=password postgres:alpine

启动信息

# docker logs postgres-test
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 ... UTC
creating configuration files ... ok
running bootstrap script ... ok
sh: locale: not found
2023-04-24 07:29:18.876 UTC [31] WARNING: no usable system locales were found
performing post-bootstrap initialization ... ok
syncing data to disk ... ok


Success. You can now start the database server using:

pg_ctl -D /var/lib/postgresql/data -l logfile start

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.
waiting for server to start....2023-04-24 07:29:22.836 UTC [37] LOG: starting PostgreSQL 14.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
2023-04-24 07:29:22.862 UTC [37] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-04-24 07:29:22.890 UTC [38] LOG: database system was shut down at 2023-04-24 07:29:21 UTC
2023-04-24 07:29:22.910 UTC [37] LOG: database system is ready to accept connections
done
server started

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

waiting for server to shut down...2023-04-24 07:29:23.015 UTC [37] LOG: received fast shutdown request
.2023-04-24 07:29:23.017 UTC [37] LOG: aborting any active transactions
2023-04-24 07:29:23.019 UTC [37] LOG: background worker "logical replication launcher" (PID 44) exited with exit code 1
2023-04-24 07:29:23.021 UTC [39] LOG: shutting down
2023-04-24 07:29:23.080 UTC [37] LOG: database system is shut down
done
server stopped

PostgreSQL init process complete; ready for start up.

2023-04-24 07:29:23.207 UTC [1] LOG: starting PostgreSQL 14.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
2023-04-24 07:29:23.208 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2023-04-24 07:29:23.208 UTC [1] LOG: listening on IPv6 address "::", port 5432
2023-04-24 07:29:23.211 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-04-24 07:29:23.238 UTC [49] LOG: database system was shut down at 2023-04-24 07:29:23 UTC
2023-04-24 07:29:23.257 UTC [1] LOG: database system is ready to accept connections

进入容器内的 shell,切换到 postgres 用户,切换到 SQL Shell(psql)

# docker exec -it postgres-test bash
bash-5.1# su postgres
/ $ psql
psql (14.1)
Type "help" for help.

postgres=#

执行 \l 查看所有数据库,\q 退出 psql

如果我们想用图形化应用来连接 docker 中的 postgresql,需要用到刚才建立容器时使用的端口。例如我刚才用的是 2345:5432,那么我应该用此时服务器的 IP 地址和 2345 端口。

我使用的是免费的 DBeaver,它在 GitHub 上面有 3 万多颗星⭐️。

「新建数据库连接」选择 postgres,填写 IP 地址、端口和密码。

postgresql-DBeaver