# 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:
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
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 端口。