diff --git a/docker/Dockerfile b/docker/Dockerfile index 0a621d9c..c8fa4139 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -68,6 +68,8 @@ RUN cp -f /app/docker/nginx.common.conf /etc/nginx/common.conf \ && cp -f /app/docker/update.sh /usr/local/bin/mp_update.sh \ && cp -f /app/docker/entrypoint.sh /entrypoint.sh \ && cp -f /app/docker/docker_http_proxy.conf /etc/nginx/docker_http_proxy.conf \ + && cp -f /app/docker/postgresql/pg_hba.conf /app/docker/postgresql/pg_hba.conf.template \ + && cp -f /app/docker/postgresql/postgresql.conf.template /app/docker/postgresql/postgresql.conf.template \ && chmod +x /entrypoint.sh /usr/local/bin/mp_update.sh \ && mkdir -p ${HOME} \ && groupadd -r moviepilot -g 918 \ diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index fb37ac53..996c4266 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -253,15 +253,18 @@ if [ "${DB_TYPE:-sqlite}" = "postgresql" ]; then su - postgres -c "initdb -D '${POSTGRESQL_DATA_DIR}'" # 配置PostgreSQL - echo "host all all 0.0.0.0/0 md5" >> "${POSTGRESQL_DATA_DIR}/pg_hba.conf" - echo "listen_addresses = '*'" >> "${POSTGRESQL_DATA_DIR}/postgresql.conf" - echo "port = ${DB_POSTGRESQL_PORT:-5432}" >> "${POSTGRESQL_DATA_DIR}/postgresql.conf" - echo "log_destination = 'stderr'" >> "${POSTGRESQL_DATA_DIR}/postgresql.conf" - echo "logging_collector = on" >> "${POSTGRESQL_DATA_DIR}/postgresql.conf" - echo "log_directory = '${POSTGRESQL_LOG_DIR}'" >> "${POSTGRESQL_DATA_DIR}/postgresql.conf" - echo "log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'" >> "${POSTGRESQL_DATA_DIR}/postgresql.conf" - echo "log_rotation_age = 1d" >> "${POSTGRESQL_DATA_DIR}/postgresql.conf" - echo "log_rotation_size = 100MB" >> "${POSTGRESQL_DATA_DIR}/postgresql.conf" + # 复制PostgreSQL配置文件 + INFO "复制PostgreSQL配置文件..." + cp /app/docker/postgresql/pg_hba.conf "${POSTGRESQL_DATA_DIR}/pg_hba.conf" + + # 使用envsubst处理postgresql.conf模板 + export POSTGRESQL_LOG_DIR="${POSTGRESQL_LOG_DIR}" + envsubst '${DB_POSTGRESQL_PORT}${POSTGRESQL_LOG_DIR}' < /app/docker/postgresql/postgresql.conf.template > "${POSTGRESQL_DATA_DIR}/postgresql.conf" + + # 设置正确的权限 + chown postgres:postgres "${POSTGRESQL_DATA_DIR}/pg_hba.conf" "${POSTGRESQL_DATA_DIR}/postgresql.conf" + chmod 600 "${POSTGRESQL_DATA_DIR}/pg_hba.conf" + chmod 644 "${POSTGRESQL_DATA_DIR}/postgresql.conf" fi # 确保目录权限正确 diff --git a/docker/postgresql/pg_hba.conf b/docker/postgresql/pg_hba.conf new file mode 100644 index 00000000..dd7a91da --- /dev/null +++ b/docker/postgresql/pg_hba.conf @@ -0,0 +1,29 @@ +# PostgreSQL Client Authentication Configuration File +# =========================================== +# +# Refer to the "Client Authentication" section in the PostgreSQL +# documentation for a complete description of this file. + +# TYPE DATABASE USER ADDRESS METHOD + +# "local" is for Unix domain socket connections only +local all all trust + +# IPv4 local connections: +host all all 127.0.0.1/32 md5 +host all all ::1/128 md5 + +# IPv6 local connections: +host all all ::1/128 md5 + +# Allow replication connections from localhost, by a user with the +# replication privilege. +local replication all trust +host replication all 127.0.0.1/32 md5 +host replication all ::1/128 md5 + +# 允许所有IPv4连接(用于Docker容器内部通信) +host all all 0.0.0.0/0 md5 + +# 允许所有IPv6连接 +host all all ::/0 md5 diff --git a/docker/postgresql/postgresql.conf.template b/docker/postgresql/postgresql.conf.template new file mode 100644 index 00000000..392e74ed --- /dev/null +++ b/docker/postgresql/postgresql.conf.template @@ -0,0 +1,65 @@ +# PostgreSQL configuration file template +# This file will be processed by envsubst to replace environment variables + +# Connection and Authentication +listen_addresses = '*' +port = ${DB_POSTGRESQL_PORT:-5432} +max_connections = 100 + +# Memory Configuration +shared_buffers = 128MB +effective_cache_size = 256MB +work_mem = 4MB +maintenance_work_mem = 64MB + +# Logging Configuration +log_destination = 'stderr' +logging_collector = on +log_directory = '${POSTGRESQL_LOG_DIR}' +log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' +log_rotation_age = 1d +log_rotation_size = 100MB +log_min_messages = warning +log_min_error_statement = error +log_min_duration_statement = 1000 + +# Query Tuning +random_page_cost = 1.1 +effective_io_concurrency = 200 + +# WAL Configuration +wal_level = replica +max_wal_size = 1GB +min_wal_size = 80MB +checkpoint_completion_target = 0.9 +wal_buffers = 16MB + +# Background Writer +bgwriter_delay = 200ms +bgwriter_lru_maxpages = 100 +bgwriter_lru_multiplier = 2.0 + +# Autovacuum +autovacuum = on +autovacuum_max_workers = 3 +autovacuum_naptime = 1min +autovacuum_vacuum_threshold = 50 +autovacuum_analyze_threshold = 50 + +# Client Connection Defaults +datestyle = 'iso, mdy' +timezone = 'UTC' +lc_messages = 'C' +lc_monetary = 'C' +lc_numeric = 'C' +lc_time = 'C' +default_text_search_config = 'pg_catalog.english' + +# Locale and Formatting +datestyle = 'iso, mdy' +timezone = 'UTC' +lc_messages = 'C' +lc_monetary = 'C' +lc_numeric = 'C' +lc_time = 'C' +default_text_search_config = 'pg_catalog.english'