阿里雲服務器 centos 7 安裝postgresql 11


Postgresql簡介
官方網站:https://www.postgresql.org/
簡介參考zhihu文章 https://www.zhihu.com/question/20010554
關於框架+DB的一些性能參考性能測試(僅供參考) http://www.techempower.com/benchmarks/#section=data-r15&hw=ph&test=fortune

一、更新rpm源

1、打開官網https://yum.postgresql.org/repopackages.php

2、復制上圖鏈接添加rpm源

$ yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

3、查看可以安裝的postgresql版本,此命令會稍微慢一些

$ yum list | grep postgresql

二、安裝postgresql

1、注意下安裝版本

yum install postgresql11-contrib postgresql11-server -y

以下是安裝日志

Dependencies Resolved

===============================================================================================================
 Package                           Arch                Version                       Repository           Size
===============================================================================================================
Installing:
 postgresql11-contrib              x86_64              11.3-1PGDG.rhel7              pgdg11              616 k
 postgresql11-server               x86_64              11.3-1PGDG.rhel7              pgdg11              4.7 M
Installing for dependencies:
 libicu                            x86_64              50.1.2-17.el7                 base                6.9 M
 libxslt                           x86_64              1.1.28-5.el7                  base                242 k
 postgresql11                      x86_64              11.3-1PGDG.rhel7              pgdg11              1.7 M
 postgresql11-libs                 x86_64              11.3-1PGDG.rhel7              pgdg11              360 k

Transaction Summary
===============================================================================================================
Install  2 Packages (+4 Dependent packages)

Total download size: 14 M
Installed size: 55 M
Downloading packages:
(1/6): libxslt-1.1.28-5.el7.x86_64.rpm                                                  | 242 kB  00:00:00     
(2/6): libicu-50.1.2-17.el7.x86_64.rpm                                                  | 6.9 MB  00:00:00     
warning: /var/cache/yum/x86_64/7/pgdg11/packages/postgresql11-contrib-11.3-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Public key for postgresql11-contrib-11.3-1PGDG.rhel7.x86_64.rpm is not installed
(3/6): postgresql11-contrib-11.3-1PGDG.rhel7.x86_64.rpm                                 | 616 kB  00:01:03     
(4/6): postgresql11-libs-11.3-1PGDG.rhel7.x86_64.rpm                                    | 360 kB  00:00:55     
postgresql11-server-11.3-1PGDG FAILED                                          2.6 kB/s | 9.4 MB  00:32:32 ETA 
https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/postgresql11-server-11.3-1PGDG.rhel7.x86_64.rpm: [Errno 12] Timeout on https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/postgresql11-server-11.3-1PGDG.rhel7.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
Trying other mirror.
(5/6): postgresql11-11.3-1PGDG.rhel7.x86_64.rpm                                         | 1.7 MB  00:06:11     
(6/6): postgresql11-server-11.3-1PGDG.rhel7.x86_64.rpm                                  | 4.7 MB  00:06:45     
---------------------------------------------------------------------------------------------------------------
Total                                                                           19 kB/s |  14 MB  00:12:57     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importing GPG key 0x442DF0F8:
 Userid     : "PostgreSQL RPM Building Project <pgsqlrpms-hackers@pgfoundry.org>"
 Fingerprint: 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
 Package    : pgdg-redhat-repo-42.0-4.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : postgresql11-libs-11.3-1PGDG.rhel7.x86_64                                                   1/6 
  Installing : libicu-50.1.2-17.el7.x86_64                                                                 2/6 
  Installing : postgresql11-11.3-1PGDG.rhel7.x86_64                                                        3/6 
  Installing : libxslt-1.1.28-5.el7.x86_64                                                                 4/6 
  Installing : postgresql11-contrib-11.3-1PGDG.rhel7.x86_64                                                5/6 
  Installing : postgresql11-server-11.3-1PGDG.rhel7.x86_64                                                 6/6 
  Verifying  : postgresql11-11.3-1PGDG.rhel7.x86_64                                                        1/6 
  Verifying  : postgresql11-server-11.3-1PGDG.rhel7.x86_64                                                 2/6 
  Verifying  : libicu-50.1.2-17.el7.x86_64                                                                 3/6 
  Verifying  : libxslt-1.1.28-5.el7.x86_64                                                                 4/6 
  Verifying  : postgresql11-libs-11.3-1PGDG.rhel7.x86_64                                                   5/6 
  Verifying  : postgresql11-contrib-11.3-1PGDG.rhel7.x86_64                                                6/6 

Installed:
  postgresql11-contrib.x86_64 0:11.3-1PGDG.rhel7         postgresql11-server.x86_64 0:11.3-1PGDG.rhel7        

Dependency Installed:
  libicu.x86_64 0:50.1.2-17.el7                       libxslt.x86_64 0:1.1.28-5.el7                           
  postgresql11.x86_64 0:11.3-1PGDG.rhel7              postgresql11-libs.x86_64 0:11.3-1PGDG.rhel7             

Complete!

查看是否安裝成功

rpm -aq| grep postgres


安裝成功后Postgresql安裝目錄是/usr/pgsql-11 文件目錄是 /var/lib/pgsql/11/data 如圖:

2、初始化數據庫

$ /usr/pgsql-11/bin/postgresql-11-setup initdb


3、設置自動啟動,並查看啟動狀態

$ systemctl enable postgresql-11
$ systemctl start postgresql-11
$ systemctl status postgresql-11

三、配置postgresql

1、登錄postgresql並設置密碼

su - postgres

進入數據庫

psql

更改密碼

ALTER USER postgres WITH PASSWORD 'postgres'

退出,並並切換回root賬戶,重新輸入密碼即可

\q
su root


2、修改訪問策略,默認是不允許遠程登錄
搜索pg_hba.conf文件

find / -name pg_hba.conf

復制文件路徑修改pg_hba.conf

vim /var/lib/pgsql/11/data/pg_hba.conf

ADDRESS:修改為samenet(匹配服務器所在子網中的任何地址) 或 0.0.0.0/0 (任意IP地址) 或 指定的IP地址
METHOD:修改為trust (可信任)


3、修改遠程訪問IP
搜索postgresql.conf文件

find / -name postgresql.conf

復制文件路徑用vim修改,取消listen_addresses前面的#注釋,並修改地址為* (所有IP) 或者線上外網IP地址,如圖
線上環境#port = 5432最好取消注釋,端口一並調整

vim /var/lib/pgsql/11/data/postgresql.conf


4、重啟postgresql-11

systemctl restart postgresql-11

四、連接postgresql

1、如果是阿里雲服務器記得打開對應的端口,否則無法連接,如果是虛擬機測試環境可忽略


2、注意檢查下防火牆是否開啟&端口是否打開,也會影響遠程連接

firewall-cmd --permanent --add-port=5432/tcp
systemctl restart firewalld

3、連接測試
navicat 官網自行下載https://www.navicat.com.cn/

pgadmin 官網自行下載https://www.pgadmin.org/download/


免責聲明!

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



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