Postgresql Linux版本安裝——RPM包安裝

realkid4發表於2016-01-22

 

PostgresqlMySQL是目前比較流行、活躍的開源關係型資料庫系統。相對於高階Oracle商業產品,PostgresqlMyQL在軟體成熟度、發展功能上的確還有很大改善空間。但是在系統選型過程中,基礎軟體水平是要受到未來系統整體負載、運維要求和重要的預算決定的。好東西是好,但也要看我們是否需要他,或者是否用得起他。

 

相對於MySQL的粗放式發展,Postgresql從最開始被Berkley研究出來,作為科研機構教學使用以來,無論是從對關係型資料庫標準的遵循,還是核心發展上,都本著比較嚴謹的態度和方式。所以,在MySQL不支援事務的時代,Postgresql是不錯的關係型資料庫產品方案。

 

目前,Postgresql目前支援包括WindowsLinuxUnix等主流作業系統平臺。在Linux上,我們可以使用三種常見的安裝方式:

 

ü  RPM包方式:從Postgresql官方網站上,直接下載Linux安裝包,通過yum或者rpm進行安裝;

ü  Yum安裝:連線PostgresqlYum庫,從網站下載所有的安裝檔案和依賴包;

ü  原始碼安裝:這種方式靈活性最大,可以顯示的自主決定安裝目錄和資料檔案位置;

 

本篇主要介紹RPM安裝包安裝方法和之後的連線配置方式。

 

1、安裝檔案準備

 

筆者使用RedHat Linux 6.4進行測試。

 

 

[root@TEST-DB uploads]# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 6.4 (Santiago)

 

 

PG官方網站下載RPM安裝包,網址:http://www.postgresql.org/download/。實驗選擇最新的9.5版本。

 

 

[root@TEST-DB uploads]# ls -l

total 6580

-rw-r--r-- 1 root root 1373788 Jan 21 08:54 postgresql95-9.5.0-2PGDG.rhel6.x86_64.rpm

-rw-r--r-- 1 root root  465492 Jan 21 08:54 postgresql95-contrib-9.5.0-2PGDG.rhel6.x86_64.rpm

-rw-r--r-- 1 root root  204948 Jan 21 08:54 postgresql95-libs-9.5.0-2PGDG.rhel6.x86_64.rpm

-rw-r--r-- 1 root root 4682132 Jan 21 08:54 postgresql95-server-9.5.0-2PGDG.rhel6.x86_64.rpm

 

 

2、安裝程式包

 

請注意安裝順序,原則上說,基礎Linux版本就可以完成安裝過程。但是對於一些包,可能需要yum程式進行支援。

 

 

[root@TEST-DB uploads]# yum install postgresql95-libs-9.5.0-2PGDG.rhel6.x86_64.rpm

Loaded plugins: product-id, refresh-packagekit, security, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

localyum                                                                 

Dependencies Resolved

 

=================================================================================================

 Package           Arch   Version              Repository                                   Size

=================================================================================================

Installing:

 postgresql95-libs x86_64 9.5.0-2PGDG.rhel6    /postgresql95-libs-9.5.0-2PGDG.rhel6.x86_64 639 k

Updating for dependencies:

 openssl           x86_64 1.0.1e-15.el6        localyum                                    1.5 M

 

Transaction Summary

=================================================================================================

Install       1 Package(s)

Upgrade       1 Package(s)

 

Total size: 2.1 M

(篇幅原因,有省略……                                                              

 

Complete!

 

 

[root@TEST-DB uploads]# yum install postgresql95-9.5.0-2PGDG.rhel6.x86_64.rpm

Loaded plugins: product-id, refresh-packagekit, security, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use

(篇幅原因,有省略……

Installed:

  postgresql95.x86_64 0:9.5.0-2PGDG.rhel6                                                       

 

Complete!

 

 

[root@TEST-DB uploads]# rpm -ivh postgresql95-server-9.5.0-2PGDG.rhel6.x86_64.rpm

warning: postgresql95-server-9.5.0-2PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY

Preparing...                ########################################### [100%]

   1:postgresql95-server    ########################################### [100%]

[root@TEST-DB uploads]# rpm -ivh postgresql95-contrib-9.5.0-2PGDG.rhel6.x86_64.rpm

warning: postgresql95-contrib-9.5.0-2PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY

Preparing...                ########################################### [100%]

   1:postgresql95-contrib   ########################################### [100%]

 

 

 

注意:選擇使用rpm包進行安裝之後,會自動建立作業系統使用者postgres,可以使用passwd命令進行密碼設定。

 

 

[root@TEST-DB uploads]# id postgres

uid=26(postgres) gid=26(postgres) groups=26(postgres)

 

[root@TEST-DB uploads]# passwd postgres

Changing password for user postgres.

New password:

BAD PASSWORD: it is based on a dictionary word

Retype new password:

passwd: all authentication tokens updated successfully.

 

 

3、本地服務配置和本地連線測試

 

使用RPM包安裝之後,系統中會自動新增一個postgresql-<vxxx>的服務。我們可以通過這個服務啟動資料庫。

 

 

--預設狀態是關閉

[root@TEST-DB uploads]# service --status-all | grep postgres

postgresql-9.5 is stopped

 

--強制啟動失效

[root@TEST-DB uploads]# service postgresql-9.5 start

/var/lib/pgsql/9.5/data is missing. Use "service postgresql-9.5 initdb" to initialize the cluster first.

[FAILED]

 

 

第一次啟動失敗,主要是由於最開始資料庫中沒有主庫存在。可以先用initdb來進行初始化動作。

 

 

[root@TEST-DB uploads]# service postgresql-9.5 initdb

Initializing database: [  OK  ]

[root@TEST-DB uploads]# service --status-all | grep postgres

postgresql-9.5 is stopped

[root@TEST-DB uploads]# service postgresql-9.5 start

Starting postgresql-9.5 service: [  OK  ]

 

 

檢視後臺postgres程式執行情況。

 

 

[root@TEST-DB uploads]# ps -ef | grep postgres

postgres 28417     1  0 09:53 ?        00:00:00 /usr/pgsql-9.5/bin/postmaster -D /var/lib/pgsql/9.5/data

postgres 28419 28417  0 09:53 ?        00:00:00 postgres: logger process                               

postgres 28421 28417  0 09:53 ?        00:00:00 postgres: checkpointer process                         

postgres 28422 28417  0 09:53 ?        00:00:00 postgres: writer process                               

postgres 28423 28417  0 09:53 ?        00:00:00 postgres: wal writer process                           

postgres 28424 28417  0 09:53 ?        00:00:00 postgres: autovacuum launcher process                  

postgres 28425 28417  0 09:53 ?        00:00:00 postgres: stats collector process                      

root     28431 27089  0 09:53 pts/0    00:00:00 grep postgres

 

 

兩個細節,標記紅色的部分是資料庫主工作程式,其中明顯表明了可執行程式和資料檔案位置。另外就是大部分程式執行使用者都是postgres,就說明雖然是通過root啟動的程式,但是root也是用postgres使用者程式啟動程式。

 

本地連線情況,在本地可以使用psql作為客戶端進行連線。

 

 

[root@TEST-DB ~]# su - postgres

-bash-4.1$ psql

psql (9.5.0)

Type "help" for help.

 

postgres=# help

You are using psql, the command-line interface to PostgreSQL.

Type:  \copyright for distribution terms

       \h for help with SQL commands

       \? for help with psql commands

       \g or terminate with semicolon to execute query

       \q to quit

postgres=#

 

postgres=# alter user postgres with password 'postgres';

ALTER ROLE

postgres=# select * from pg_shadow;

 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |               passwd   

            | valuntil | useconfig

----------+----------+-------------+----------+---------+--------------+-------------------------

------------+----------+-----------

 postgres |       10 | t           | t        | t       | t            | md53175bce1d3201d16594ce

bf9d7eb3f9d |          |

(1 row)

 

postgres-# \q

-bash-4.1$ exit

logout

 

 

根據標準psql命令,後面應當寫上連線資料庫的名稱。如果沒有寫,就預設連線名稱為postgres的資料庫,這個庫就是在initdb執行過程中建立好的內容。

 

4、遠端連線配置

 

注意:我們當前所有操作都是在資料庫伺服器上進行的操作。預設情況下,Postgresql對於遠端訪問連線時拒絕的。我們需要進行額外的配置專案。

 

MySQL相似,PG的很多配置引數都是散佈在文字格式檔案中的。在配置網路連線中,我們需要修改兩個檔案。

 

首先是PG資料檔案下的pg_hba.conf

 

 

[root@TEST-DB ~]# su - postgres

-bash-4.1$ cd /var/lib/pgsql/9.5/data/

 

-bash-4.1$ ls -l | grep conf

-rw------- 1 postgres postgres  4224 Jan 21 09:53 pg_hba.conf

-rw------- 1 postgres postgres  1636 Jan 21 09:53 pg_ident.conf

-rw------- 1 postgres postgres    88 Jan 21 09:53 postgresql.auto.conf

-rw------- 1 postgres postgres 21738 Jan 21 09:53 postgresql.conf

 

 

pg_hba.conf內容很多,我們需要修改其中關於允許連線站點的設定。

 

 

# "local" is for Unix domain socket connections only

local   all             all                                     peer

# IPv4 local connections:

host    all             all             127.0.0.1/32            ident

# IPv6 local connections:

host    all             all             ::1/128                 ident

# Allow replication connections from localhost, by a user with the

# replication privilege.

#local   replication     postgres                                peer

#host    replication     postgres        127.0.0.1/32            ident

#host    replication     postgres        ::1/128                 ident

 

 

IPv4 Local connection部分進行配置。

 

 

# IPv4 local connections:

host    all             all             127.0.0.1/32            ident

host    all             all             172.17.107.0/24         password

host    all             all             172.17.197.0/24         password

 

 

連線IP地址後面的/24表示可以支援該網段所有IP地址訪問。Password表示通過使用者名稱密碼驗證方式連線。

 

另一個配置檔案是postgresql.conf,其中定義了監聽程式listener的監聽範圍。預設情況下取值如下:

 

 

# - Connection Settings -

#listen_addresses = 'localhost'         # what IP address(es) to listen on;

                           # comma-separated list of addresses;

                           # defaults to 'localhost'; use '*' for all

                           # (change requires restart)

#port = 5432                            # (change requires restart)

max_connections = 100                   # (change requires restart)

 

 

修改listen_addresses專案,去除掉註釋資訊,將localhost變為*。這樣就控制監聽程式監聽來自所有IP地址的連線請求了。

 

 

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;

                         # comma-separated list of addresses;

                          # defaults to 'localhost'; use '*' for all

                                        # (change requires restart)

#port = 5432                            # (change requires restart)

max_connections = 100                   # (change requires restart)

 

 

重新啟動PG程式。

 

 

[root@TEST-DB ~]# service postgresql-9.5 restart

Stopping postgresql-9.5 service: [  OK  ]

Starting postgresql-9.5 service: [  OK  ]

 

 

在遠端Windows伺服器上,我們通過pgAdmin客戶端工具配置好連線介面。

 

Postgresql Linux版本安裝——RPM包安裝

 

點選連線,可以確認成功。

 

Postgresql Linux版本安裝——RPM包安裝

 

5、結論

 

PG是目前比較流行的開源關聯式資料庫產品。對於大多數的行業和企業而言,關係型資料庫其實還是佔到需求的主流位置的。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17203031/viewspace-1980628/,如需轉載,請註明出處,否則將追究法律責任。

相關文章