我正在使用 CentOS 7.3 作为来自 Oracle VirtualBox 的 guest VM。主机是 Windows 7,我有一个物理 USB 内存棒(又名闪存驱动器)来存放 Postgres 的数据目录。我可以使用 U 盘作为 Linux 中的挂载目录。我可以向其中读取和写入文件。
我希望能够在我的 U 盘上安装 Postgres 数据库。但是我无法在我的 U 盘上获取 Postgres 数据库。我在 Linux VM 上安装了 Postgres。
要更改 Postgres 的默认数据目录,我遵循了这些 directions .
如果你没有时间去链接,我只是用这两个命令安装了 Postgres:
yum -y install postgresql-server postgresql-contrib
postgresql-setup initdb
然后我运行了这两个命令:
mv /var/lib/pgsql/data/* /mnt/windows-share/data
ln -s /mnt/windows-share /var/lib/pgsql/data
然后我尝试启动 Postgres 服务,但出现错误。
这是我尝试的命令(作为 root):
systemctl 启动 postgresql
这里是错误:
Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for detail.
我尝试了 systemctl status postgresql.service
,我发现了这个:
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled vendorpreset: disabled)
Active: failed (Result exit-code)... Process ... ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=1/FAILURE)
... failed to start PostgreSQL... ...Unit postgresql.service... ...postgresql.service failed
我使用了 journalctl -xe
但这并没有告诉我任何有意义的信息。
要更改 Postgres 的默认数据目录,我试过这个:
postgresql-setup initdb --pgdata=/mnt/windows-share/
但是我得到了
failed to find PGDATA setting in --pgdata=/mnt/mar/data.service
如何使用自定义数据目录安装 Postgres?我需要它位于“/mnt/”目录中。我想在 U 盘上创建我所有的数据库。
最佳答案
要在自定义位置(例如 home/postgres 目录)安装 Postgres,请按照以下步骤操作
安装所需版本的 Postgres,例如13 在 Home 中创建 postgres 目录。并将其访问权限授予 posgres 用户
mkdir postgres
chown postgres:postgres postgres
usermod -m -d /home/postgres postgres
将数据目录移动到新位置:
rsync -av /var/lib/pgsql/ /home/postgres/
将 postgres 服务器中的位置更新为:
find / -name postgresql-9.5.service
vi /usr/lib/systemd/system/postgresql-9.5.service
更新文件中的数据位置:
从旧位置,例如
# Location of database directory
Environment=PGDATA=/var/lib/pgsql/13/data/
到新位置,例如
# Location of database directory
Environment=PGDATA=/home/postgres/13/data/"
停止postgres,重新加载守护进程并启动postgres
stop postgres
systemctl daemon-reload
start postgres
完成...
关于linux - 如何自定义 Postgres 数据库在 Linux 服务器上的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42382067/