一、拉取鏡像
docker pull postgres:13
二、簡單版啓動容器
1. 執行命令啓動容器
docker run --name postgres -e POSTGRES_PASSWORD=1234 -p 5433:5432 -d postgres:13
2. 打開navicat查看結果
從以上結果看出數據庫連接成功
3. 拷貝鏡像默認配置到宿主機
docker cp postgres:/var/lib/postgresql/data/postgresql.conf d:/usr/share/postgresql/postgresql.conf
4. 查看配置文件,刪除掉註釋行後,鏡像默認配置的結果如下
listen_addresses = '*'
# port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
max_wal_size = 1GB
min_wal_size = 80MB
log_timezone = 'Etc/UTC'
datestyle = 'iso, mdy'
timezone = 'Etc/UTC'
# These settings are initialized by initdb, but they can be changed.
lc_messages = 'en_US.utf8' # locale for system error message
lc_monetary = 'en_US.utf8' # locale for monetary formatting
lc_numeric = 'en_US.utf8' # locale for number formatting
lc_time = 'en_US.utf8' # locale for time formatting
# default configuration for text search
default_text_search_config = 'pg_catalog.english'
5. 刪除容器
docker rm -f postgres
三、複雜版啓動容器
1. 在D:\usr\bin目錄下創建docker-compose-postgres.yml文件
version: '3.1'
services:
# 服務器名
db:
image: postgres:13
container_name: postgres
# restart: always
# 從指定配置文件啓動
command: -c 'config_file=/etc/postgresql/postgresql.conf'
volumes:
- pgdata:/var/lib/postgresql/data
- /d/usr/share/postgresql/log:/var/log/postgresql
# # 配置文件默認路徑 /var/lib/postgresql/data/postgresql.conf
- /d/usr/share/postgresql/my-postgres.conf:/etc/postgresql/postgresql.conf
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: test
ports:
- 5433:5432
adminer:
image: adminer:4.7.7-standalone
container_name: adminer
# restart: always
ports:
- 8080:8080
volumes:
pgdata:
external: true
2. 在D:\usr\bin目錄下創建postgres啓動腳本postgres-up.sh
#!/bin/bash
docker-compose -f d:/usr/bin/docker-compose-postgres.yml up
3. 在目錄下D:\usr\share\postgresql創建配置文件my-postgres.conf
listen_addresses = '*'
# port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
shared_buffers = 128MB # min 128kB
dynamic_shared_memory_type = posix # the default is the first option
max_wal_size = 1GB
min_wal_size = 80MB
log_timezone = 'Etc/UTC'
datestyle = 'iso, mdy'
timezone = 'Etc/UTC'
# These settings are initialized by initdb, but they can be changed.
lc_messages = 'en_US.utf8' # locale for system error message
lc_monetary = 'en_US.utf8' # locale for monetary formatting
lc_numeric = 'en_US.utf8' # locale for number formatting
lc_time = 'en_US.utf8' # locale for time formatting
# default configuration for text search
default_text_search_config = 'pg_catalog.english'
4. 手動創建數據卷
docker volume create --name=pgdata
5. 切換到postgres-up.sh腳本腳本所在目錄,執行腳本命令
cd d:/usr/bin
./postgres-up.sh
6. 查看結果
從以上結果可以看出配置成功
四、日誌配置
1. 配置文件my-postgres.conf新增內容
logging_collector = on
log_destination = 'stderr'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_directory = '/var/log/postgresql'
log_statement = all #需設置跟蹤所有語句,否則只能跟蹤出錯信息,有4種類型:none(默認), ddl, mod, all
log_min_duration_statement = 5000 #milliseconds,記錄執行5秒及以上的語句
2. 重啓容器
./postgres-up.sh
3. 執行錯誤查詢語句生成日誌
# 正確表名爲pg_stat_activity
select * from pg_stat_activity1 where state<>'idle' and now()-query_start > interval '1 s' order by query_start
4. 查看D:\usr\share\postgresql\log目錄下日誌文件內容
2020-05-28 09:03:52.208 UTC [1] LOG: starting PostgreSQL 13beta1 (Debian 13~beta1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-05-28 09:03:52.208 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2020-05-28 09:03:52.208 UTC [1] LOG: listening on IPv6 address "::", port 5432
2020-05-28 09:03:52.484 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-05-28 09:03:52.626 UTC [27] LOG: database system was shut down at 2020-05-28 09:03:47 UTC
2020-05-28 09:03:52.706 UTC [1] LOG: database system is ready to accept connections
2020-05-28 09:04:23.926 UTC [35] LOG: statement: set client_encoding to 'UNICODE'
2020-05-28 09:04:23.928 UTC [35] LOG: statement: SELECT DISTINCT datlastsysoid FROM pg_database
2020-05-28 09:04:23.930 UTC [35] LOG: statement: SHOW datestyle
2020-05-28 09:04:23.932 UTC [35] LOG: statement: SELECT d.oid, d.datname AS databasename, d.datacl, d.datistemplate, d.datallowconn, pg_get_userbyid(d.datdba) AS databaseowner, d.datcollate, d.datctype, shobj_description(d.oid, 'pg_database') AS description, d.datconnlimit, t.spcname, d.encoding, pg_encoding_to_char(d.encoding) AS encodingname FROM pg_database d LEFT JOIN pg_tablespace t ON d.dattablespace = t.oid
2020-05-28 09:04:26.018 UTC [35] LOG: statement: SELECT DISTINCT datlastsysoid FROM pg_database
2020-05-28 09:04:26.020 UTC [35] LOG: statement: SHOW datestyle
2020-05-28 09:04:26.022 UTC [35] LOG: statement: SELECT d.oid, d.datname AS databasename, d.datacl, d.datistemplate, d.datallowconn, pg_get_userbyid(d.datdba) AS databaseowner, d.datcollate, d.datctype, shobj_description(d.oid, 'pg_database') AS description, d.datconnlimit, t.spcname, d.encoding, pg_encoding_to_char(d.encoding) AS encodingname FROM pg_database d LEFT JOIN pg_tablespace t ON d.dattablespace = t.oid
2020-05-28 09:04:29.066 UTC [35] LOG: statement: select * from pg_stat_activity1 where state<>'idle' and now()-query_start > interval '1 s' order by query_start
2020-05-28 09:04:29.066 UTC [35] ERROR: relation "pg_stat_activity1" does not exist at character 15
2020-05-28 09:04:29.066 UTC [35] STATEMENT: select * from pg_stat_activity1 where state<>'idle' and now()-query_start > interval '1 s' order by query_start
到此日誌配置成功