postgrep修改存储目录

此篇文档为转载,来自赵熠东的csdn博客,地址暂时未找到
安装yum源
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
 
安装客户端和服务端
yum install -y postgresql10-server postgresql10
安装完会在系统中建立postgres用户,并在其.bash_profile中设置PGDATA=/var/lib/pgsql/10/data
在/usr/lib/systemd/system/目录建立postgresql-10.service用于支持 systemd调用
 
systemd设置开机启动原理
支持 systemd启动的程序会在/usr/lib/systemd/system/下创建.service启动脚本
systemctl enable postgresql-10.service
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-10.service to /usr/lib/systemd/system/postgresql-10.service.
设置开机启动就是在/etc/systemd/system/multi-user.target.wants/设置/usr/lib/systemd/system/对应service的符号连接
systemctl disable postgresql-10.service
Removed symlink /etc/systemd/system/multi-user.target.wants/postgresql-10.service
取消开机启动就是在/etc/systemd/system/multi-user.target.wants/删除对应service的符号连接
数据库初始化脚本postgresql-10-setup会读取/usr/lib/systemd/system/postgresql-10.service脚本里面的PGDATA用来设置数据库文件的存放位置
 
建立数据库数据文件存放目录
mkdir -p /data/pgsql/10/data/
chown postgres /data/pgsql -R
 
修改配置文件
使用root用户修改/usr/lib/systemd/system/postgresql-10.service的PGDATA路径
vim /usr/lib/systemd/system/postgresql-10.service
Environment=PGDATA=/var/lib/pgsql/10/data/
改成
Environment=PGDATA=/data/pgsql/10/data/
而后
systemctl daemon-reload
从新加载配置文件
 
修改postgres用户的~/.bash_profile的环境变量PGDATA为实际路径(不修改这个对于整个安装过程没有任何影响)
su - postgres
vim ~/.bash_profile
PGDATA=/var/lib/pgsql/10/data
改成
PGDATA=/data/pgsql/10/data
 
确认配置文件
postgresql-10-setup初始化脚本,会经过
systemctl show -p Environment "postgresql-10.service" |
                sed 's/^Environment=//' | tr ' ' '\n' |
                sed -n 's/^PGDATA=//p' | tail -n 1
获取数据库文件存放的位置,执行以上命令,若是显示的路径跟设置的路径不一致,就须要执行
systemctl daemon-reload
从新加载配置文件,再次查看,若是路径还不对,就说明设置的路径有问题。
 
初始化数据库
使用root用户执行
/usr/pgsql-10/bin/postgresql-10-setup initdb
初始化数据库后会在/data/pgsql/10/data/建立数据库相关的数据文件和 配置文件
而且会将数据库文件存放的目录/data目录权限设为0700,因此若是要迁移到其余路径,也应该将该目录设为 chmod 0700,不然启动会报错
 
开启远程访问
修改配置文件postgresql.conf
vim /data/pgsql/10/data/postgresql.conf
修改#listen_addresses = 'localhost' 为 listen_addresses='*' (注意须要删除#注释)
固然,此处‘*’也能够改成任何你想开放的服务器IP
 
信任远程链接
修改配置文件pg_hba.conf
vim /data/pgsql/10/data/pg_hba.conf 
使用shift+g跳至底部
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv4 local connections:
host    all             all             0.0.0.0/0               md5
0.0.0.0/0表示全部IP可链接,也能够设置为特定IP
 
设置开机启动
systemctl enable postgresql-10
启动数据库
systemctl start postgresql-10
 
修改数据库管理员密码
su - postgres
psql
输入
\password
或者
\password postgres
 
防火墙开放5432端口
CentOS 防火墙中内置了PostgreSQL服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,咱们只需以服务方式将PostgreSQL服务开放便可
firewall-cmd --add-service=postgresql --permanent  开放postgresql服务
firewall-cmd --reload  重载防火墙
 
 
参考地址