更改数据目录后,postgresql无法启动

16

我在Debian上使用PostgreSQL。 在我编辑配置文件后,PostgreSQL服务无法启动:

#data_directory = '/var/lib/postgresql/9.4/main'                # use data in another directory
data_directory = '/opt/data/postgresql/data'

(是的,我只是使用自定义目录而不是默认的data_directory)

我在/var/log/syslog中找到了日志。

Sep 14 10:22:17 thinkserver-ckd postgresql@9.4-main[11324]: Error: could not exec /usr/lib/postgresql/9.4/bin/pg_ctl /usr/lib/postgresql/9.4/bin/pg_ctl start -D /opt/data/postgresql/data -l /var/log/postgresql/postgresql-9.4-main.log -s -o  -c config_file="/etc/postgresql/9.4/main/postgresql.conf" :
Sep 14 10:22:17 thinkserver-ckd systemd[1]: postgresql@9.4-main.service: control process exited, code=exited status=1
Sep 14 10:22:17 thinkserver-ckd systemd[1]: Failed to start PostgreSQL Cluster 9.4-main.
Sep 14 10:22:17 thinkserver-ckd systemd[1]: Unit postgresql@9.4-main.service entered failed state.

/var/log/postgresql/postgresql-9.4-main.log中没有任何内容。

谢谢。


我最终得到了这个答案:

PostgreSQL中这个错误的含义是什么?

@langton的回答是:

  

你应该运行pg_upgradecluster或类似命令,或者只需使用pg_createcluster创建新群集(这些命令适用于Debian系统 - 您没有指定您的OS)。

所以我执行了以下命令:

pg_createcluster -d /opt/data/postgresql/data -l /opt/data/postgresql/log 9.4 ckd

然后是: service postgresql restart

它启动了!


检查目录以确保Postgres对目录及其内容具有读/写/执行权限。我不确定Debian是否使用SELINUX,但如果使用,则确保文件夹具有正确的上下文,以便Postgres可以访问它。 (快速检查SELINUX是否涉及的一种方法是将其暂时设置为宽容模式,并查看是否解决了问题) - PabTorre
1
@PabTorre 我已经检查了数据目录的权限,我确定这不是问题所在。原因只是我需要对数据库进行初始化。还是非常感谢你。 - Billy Yang
为什么不在安装时指定此目录? - VynlJunkie
1个回答

0

如果允许停机,并且您已经在旧群集位置拥有带有数据的数据库,则只需要将数据物理复制到新位置。

如果分区空间不足,则这是一种更常见的操作

# Check that current data directory is the same that
# the one in the postgresql.conf config file
OLD_DATA_DIR=$(sudo -u postgres psql --no-psqlrc --no-align --tuples-only --quiet -c "SHOW data_directory;")
echo "${OLD_DATA_DIR}"

CONFIG_FILE=$(sudo -u postgres psql --no-psqlrc --no-align --tuples-only --quiet -c "SHOW config_file;")
echo "${CONFIG_FILE}"

# Stop PostgreSLQ
systemctl stop postgresql

# Change the data directory in the config
# Better to do it with an editor, instead of sed
NEW_DATA_DIR='/opt/data/postgresql/data'
sed -i "s%data_directory = '${OLD_DATA_DIR}'%data_directory = '${NEW_DATA_DIR}'%" "${CONFIG_FILE}"

# Move/Copy the data for example using rsync
rsync -av --dry-run "${OLD_DATA_DIR}" "${NEW_DATA_DIR}"

# Take care with the classical issues of rsync and end backslashes
rsync -av "${OLD_DATA_DIR}" "${NEW_DATA_DIR}"

# Rename the old dir, just to avoid missunderstandings and set
# check the permissions on the new one

# Start postgres
systemctl  start postgresql

# Check that everything goes well and eventually drop the old data
# Make sure that the logs and everything else is where you want.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接