CentOS7安裝並配置PostgreSQL


CentOS7安裝並配置PostgreSQL

步驟總結

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
firewall
-cmd --add-service=postgresql --permanent firewall-cmd --reload
su
- postgres psql -U postgres ALTER USER postgres with encrypted password 'abc123'; \q exit
vi
/var/lib/pgsql/9.6/data/postgresql.conf vi /var/lib/pgsql/9.6/data/pg_hba.conf systemctl restart postgresql-9.6.service

步驟詳情

安裝yum源

打開https://yum.postgresql.org/repopackages.php ,找到自己需要的版本。 

以root模式進入CentOS7,輸入yum install后面加上剛剛復制的鏈接,回車。 

yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

安裝PostgreSQL

yum install -y postgresql96-server postgresql96-contrib
// 如果使用其他版本的PostgreSQL則需要把其中的兩個96換成對應的數字

輸入/usr/pgsql-9.6/bin/postgresql96-setup initdb並回車,初始化數據庫。 
(如果使用其他版本的PostgreSQL則需要把其中的9.696換成對應的數字) 

 

輸入systemctl start postgresql-9.6並回車,啟動服務。 
輸入systemctl enable postgresql-9.6並回車,設為開機自啟。 
(如果使用其他版本的PostgreSQL則需要把其中的兩個9.6換成對應的版本) 

 

(如果未安裝firewalld防火牆可跳過下面兩步) 
輸入firewall-cmd --add-service=postgresql --permanent並回車,開放防火牆。 
輸入firewall-cmd --reload並回車,重啟防火牆。

 

修改默認PostgreSQL用戶密碼
PostgreSQL安裝后會創建一個用戶,名為postgres。
輸入su - postgres並回車,切換至用戶。
輸入psql -U postgres並回車,登錄數據庫。
輸入ALTER USER postgres with encrypted password 'abc123';(不要漏了“;”)並回車,設置默認用戶postgre的密碼,此處密碼為abc123,可自行修改。
輸入\q並回車, 退出數據庫。
輸入exit並回車,退出用戶。

 

配置遠程訪問

輸入vi /var/lib/pgsql/9.6/data/postgresql.conf並回車。 
(如果使用其他版本的PostgreSQL則需要把其中的9.6換成對應的版本) 
光標下翻,找到listen_addresses。 

按i鍵進入插入編輯模式,如果想對所有IP開放,則將localhost改為*即可,如果想僅對部分IP開放,多個IP之間用,(逗號+空格)隔開。 
改完之后去掉“listen_address”前面的#。 

 

編輯完成后,按Esc鍵,輸入:wq並回車。 
輸入vi /var/lib/pgsql/9.6/data/pg_hba.conf並回車,將光標移至底部。 
(如果使用其他版本的PostgreSQL則需要把其中的9.6換成對應的版本) 

 

按i鍵進入插入編輯模式,在IPv4 local connections下方添加允許連接的IP。 
如果想允許所有IPv4地址,則加入一行host all all 0.0.0.0/0 md5。IPv6方法類似。 

編輯完成后,按Esc鍵,輸入:wq並回車。 
輸入systemctl restart postgresql-9.6.service並回車,重啟服務。 
(如果使用其他版本的PostgreSQL則需要把其中的9.6換成對應的版本)

 

重要!!!

firewall-cmd --zone=public --list-ports

firewall-cmd --zone=public --add-port=5432/tcp --permanent          // 5432為postgresql端口

firewall-cmd --reload

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM