如何强制PostgreSQL启动时读取配置文件?设置shared_buffers等参数
PostgreSQL共享内存分配错误及配置生效问题解决
问题场景
启动PostgreSQL服务器时触发以下错误:
2023-02-28 03:27:57.896 UTC [64390] FATAL: could not map anonymous shared memory: Cannot allocate memory 2023-02-28 03:27:57.896 UTC [64390] HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded available memory, swap space, or huge pages. To reduce the request size (currently 17214251008 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections. 2023-02-28 03:27:57.896 UTC [64390] LOG: database system is shut down
修改/etc/postgresql/13/main/postgresql.conf中的参数(包括将shared_buffer改为128kB、max_connections改为5,及多个中间值)后,错误信息无任何变化,配置未生效。
关键问题排查
首先注意:你配置文件中的shared_buffer是错误参数名,正确的参数为shared_buffers(复数形式)。PostgreSQL会忽略未知参数,继续使用默认值,这大概率是配置不生效的核心原因。
解决步骤
1. 修正配置文件参数名
打开/etc/postgresql/13/main/postgresql.conf,将错误的参数名修正:
shared_buffers = 128kB max_connections = 5
保存后验证参数是否能被正确读取:
postgres -D /var/lib/postgresql/13/main -C shared_buffers postgres -D /var/lib/postgresql/13/main -C max_connections
返回值应为你设置的128kB和5。
2. 确认配置文件加载路径
PostgreSQL可能加载了其他路径的配置文件,执行以下命令查看当前实例实际使用的配置文件:
ps aux | grep postgres | grep config_file
如果是systemd管理的服务,检查服务文件中的配置路径:
cat /etc/systemd/system/postgresql@13-main.service | grep PGCONF
确保路径指向你修改的/etc/postgresql/13/main/postgresql.conf。
3. 显式指定配置文件启动
启动时直接指定配置文件路径,强制加载修改后的配置:
pg_ctl -D /var/lib/postgresql/13/main -c config_file=/etc/postgresql/13/main/postgresql.conf start
替换-D后的路径为你的PostgreSQL数据目录(通常与配置文件同目录下的main文件夹)。
4. 临时通过命令行传递参数
若配置文件仍无法生效,启动时直接传递参数跳过配置文件:
pg_ctl -D /var/lib/postgresql/13/main start -o "-c shared_buffers=128kB -c max_connections=5"
5. 系统服务方式的生效步骤
如果使用systemd管理服务,修改配置后需重新加载服务配置并重启:
sudo systemctl daemon-reload sudo systemctl restart postgresql@13-main.service
内容的提问来源于stack exchange,提问作者Kwahn
相关产品推荐
相关产品推荐

