1、官网网站
https://www.postgresql.org/download/linux/redhat/
https://www.postgresql.org/ftp/source/
2、先安装PostgreSQL的YUM源
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
3、安装server
yum install -y postgresql13-server
4、初始化数据库
/usr/pgsql-13/bin/postgresql-13-setup initdb
PostgreSQL 初始化数据库之后,默认的数据目录是在/var/lib/pgsql
移动原始数据目录
mv /var/lib/pgsql /data/
创建软连
cd /var/lib && ln -s /data/databases/pgsql pgsql
5、服务
# 可以选择性的开启 开机自启动
#systemctl enable postgresql-13
#启动服务
systemctl start postgresql-13
#重启服务
systemctl restart postgresql-13
#停止数据库
systemctl stop postgresql-13
6、切换到 postgres 用户(yum安装时自动生成)
su - postgres
直接输入 psql 回车
-bash-4.2$ psql
psql (9.2.24, server 13.12)
WARNING: psql version 9.2, server version 13.0.
Some psql features might not work.
Type “help” for help.
postgres=#
设置postgres用户密
ALTER USER postgres WITH PASSWORD ‘123456’
退出psql
postgres=# \q
-bash-4.2$
退出postgres 用户
-bash-4.2$ exit
logout
7、默认情况下PostgreSQL不支持密码登录,如需支持需要修改配置文件
vim /var/lib/pgsql/13/data/pg_hba.conf
将未注释行中的ident 替换为 md5
8、如需开启远程访问
vim /var/lib/pgsql/13/data/postgresql.conf
将 #listen_addresses = ‘localhost’
修改为 listen_addresses='’ (
当然,此处‘’也可以改为任何你想开放的服务器IP)
另外对于特定的IP还可以设置开启信任远程连接,修改/var/lib/pgsql/13/data/pg_hba.conf,按下面的格式进行添加设置。
#IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 8.8.8.8/32(需要连接的服务器IP) trust
修改完配置以后不要忘了重启服务。
9、打开防火墙
CentOS 防火墙中内置了PostgreSQL服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,只需以服务方式将PostgreSQL服务开放即可。
firewall-cmd --add-service=postgresql --permanent
firewall-cmd --reload
firewall-cmd --list-all
https://zhuanlan.zhihu.com/p/590852929
http://www.taodudu.cc/news/show-1207417.html
http://www.taodudu.cc/news/show-283999.html?action=onClick