Centos7安裝postgresql9.6
第一步:下載庫
以root模式進入CentOS7,輸入
yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
第二步:安裝
執行
yum install -y postgresql96-server postgresql96-contrib
第三步:初始化數據庫
執行
`/usr/pgsql-9.6/bin/postgresql96-setup initdb`
第四步:重啓服務
systemctl start postgresql-9.6
第五步:設爲開機自啓
systemctl enable postgresql-9.6
第六步:修改默認密碼
輸入 su - postgres並回車,切換至用戶。
輸入 psql -U postgres並回車,登錄數據庫。
輸入 ALTER USER postgres with encrypted password 'postgres';(不要漏了“;”)並回車
輸入 \q並回車, 退出數據庫。
輸入 exit並回車,退出用戶。
第七步:配置遠程訪問
1.修改postgresql.conf文件
輸入
vi /var/lib/pgsql/9.6/data/postgresql.conf
找到listen_addresses ,將該條解釋,並且修改爲 * 即可 (輸入i進入編輯)
編輯完成後,按Esc鍵,輸入:wq並回車。
2. 修改pg_hba.conf文件
輸入
vi /var/lib/pgsql/9.6/data/pg_hba.conf
並回車,將光標移至底部。
按i鍵進入插入編輯模式,在IPv4 local connections下方添加允許連接的IP。
如果想允許所有IPv4地址,則加入一行host all all 0.0.0.0/0 md5。
編輯完成後,按Esc鍵,輸入:wq並回車。
第八步:重啓服務
輸入
systemctl restart postgresql-9.6.service
並回車
注: 可以在外網測試連接postgres數據庫,如果連接上就不用配置防火牆,如果不可以的話請執行下面兩步。
1.輸入firewall-cmd --add-service=postgresql --permanent並回車,開放防火牆。
2.輸入firewall-cmd --reload並回車,重啓防火牆。