前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >postgrep修改存储目录

postgrep修改存储目录

作者头像
东营浪人
发布2019-12-20 11:23:38
2.4K0
发布2019-12-20 11:23:38
举报
文章被收录于专栏:浪人聊编程

此篇文档为转载,来自赵熠东的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 重载防火墙

参考地址

https://www.postgresql.org/download/linux/redhat/

https://www.itzgeek.com/how-tos/linux/centos-how-tos/install-postgresql-9-3-on-centos-7.html

https://tecadmin.net/install-postgresql-server-centos/

https://www.jianshu.com/p/6f9110921b59

https://www.cnblogs.com/stulzq/p/7766409.html

https://blog.csdn.net/shanzhizi/article/details/50662286

https://www.cnblogs.com/think8848/p/5877076.html

http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档