如何迁移PostgreSQL的数据目录?
- By : Will
- Category : PostgreSQL
PostgreSQL
1 前言
一个问题,一篇文章,一出故事。
笔者今天完成最新版本的PostgreSQL服务端的安装配置,现在需要完成修改数据库默认存储的测试。
2 最佳实践
2.1 测试环境
2.2 迁移操作
2.2.1 停止服务
systemctl stop postgresql.service
2.2.2 迁移数据目录
mkdir -p /data mv /var/lib/pgsql/data /data/pgsql ln -s /data/pgsql /var/lib/pgsql/data
2.2.3 修改服务控制脚本(可选)
vim /usr/lib/systemd/system/postgresql.service
配置修改如下,
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. It is recommended to use systemd
# "dropin" feature; i.e. create file with suffix .conf under
# /etc/systemd/system/UNITNAME.service.d directory overriding the
# unit's defaults. Look at systemd.unit(5) manual page for more info.
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=notify
User=postgres
Group=postgres
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
# ... but allow it still to be effective for child processes
# (note that these settings are ignored by Postgres releases before 9.5)
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0
Environment=PGDATA=/data/pgsql
ExecStartPre=/usr/libexec/postgresql-check-db-dir %N
# Even though the $PGDATA variable is exported (postmaster would accept that)
# use the -D option here so PGDATA content is printed by /bin/ps and by
# 'systemctl status'.
ExecStart=/usr/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
# No artificial start/stop timeout (rhbz#1525477, pgrpms#2786).
TimeoutSec=0
[Install]
WantedBy=multi-user.target
配置修改后,你需要执行如下命令重载服务使配置生效,
systemctl daemon-reload
2.2.4 启动服务
systemctl start postgresql.service
没有评论