哈特中尉's Blog

不会写代码的司机不是好厨师!

centos7安装postgresql10.1

官方教程:https://www.postgresql.org/download/linux/redhat/

安装资源rpm

1
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-1.noarch.rpm

安装客户端

1
yum -y install postgresql10

安装服务端

1
yum -y install postgresql10-server

初始化数据库

1
/usr/pgsql-10/bin/postgresql-10-setup initdb

修改配置文件

vi /var/lib/pgsql/10/data/postgresql.conf
一共修改3处

  • #listen_addresses = 'localhost' 改为 listen_addresses = '*'
  • #port = 5432 改为 port = 5432
  • #password_encryption = md5 改为 password_encryption = md5

修改访问控制文件

vi /var/lib/pgsql/10/data/pg_hba.conf
文件最后一行加入
host all all 0.0.0.0/0 md5

开放防火墙

1
2
3
firewall-cmd --permanent --add-port=5432/tcp  
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

启动服务

1
systemctl start postgresql-10

访问postgresql

1
su - postgres

设置postgres用户密码

  • su - postgres 切换用户,执行后提示符会变为 ‘-bash-4.2$’
  • psql -U postgres 登录数据库,执行后提示符变为 ‘postgres=#’
  • ALTER USER postgres WITH PASSWORD 'abc123';  设置postgres用户密码
  • \q  退出数据库
  • exit

重启服务

1
systemctl restart postgresql-10

开机自启动

1
systemctl enable postgresql-10