1. 程式人生 > >1. PostgreSQL-安裝和基本配置(學習筆記)

1. PostgreSQL-安裝和基本配置(學習筆記)

安裝和配置 日常使用 buffer java、 note 安裝完成 for ora har

1 PostgreSQL簡介

1.1 概述

??PostgreSQL數據庫是目前功能最強大的開源數據庫,支持豐富的數據類型(如JSON和JSONB類型,數組類型)和自定義類型。而且它提供了豐富的接口,可以很容易地擴展它的功能,如可以在GiST框架下實現自己的索引類型等,它還支持使用C語言寫自定義函數、觸發器,也支持使用流行的語言寫自定義函數、比如其中的PL/Perl提供了使用Perl語言寫自定義函數的功能,當然還有PL/Python,PL/Tcl,等等。

1.2 PostgreSQL優勢

  • PostgreSQL數據庫是目前功能最強大的開源數據庫,它是最接近工業標準SQL92的查詢語言,並且正在實現新的功能以兼容最新的SQL標準:SQL2003。

  • 穩定可靠:PostgreSQL是唯一能夠做到數據零丟失的開源數據庫。有報道稱國外部分銀行也在使用PostgreSQL數據庫。

  • 開源省錢:PostgreSQL數據庫是開源的、免費的,而且是BSD協議,在使用和二次開發上基本沒有限制。

  • 支持廣泛:PostgreSQL數據庫支持大量的主流開發語言,包括C、C++、Perl、Python、Java、Tcl,以及PHP等。

  • PostgreSQL社區活躍:PostgreSQL基本上每三個月推出一個補丁版本,這意味著已知的BUG很快會被修復,有應用場景的需求也會及時得到響應。

2 PostgreSQL安裝和配置

2.1 yum源中包含的PostgreSQL包

[root@localhost ~]# yum list | grep postgresql
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
freeradius-postgresql.x86_64               3.0.4-6.el7                 local    
libreoffice-postgresql.x86_64              1:5.0.6.2-3.el7             local    
pcp-pmda-postgresql.x86_64                 3.11.3-4.el7                local    
postgresql.i686                            9.2.18-1.el7                local    
postgresql.x86_64                          9.2.18-1.el7                local    
postgresql-contrib.x86_64                  9.2.18-1.el7                local    
postgresql-devel.i686                      9.2.18-1.el7                local    
postgresql-devel.x86_64                    9.2.18-1.el7                local    
postgresql-docs.x86_64                     9.2.18-1.el7                local    
postgresql-jdbc.noarch                     9.2.1002-5.el7              local    
postgresql-jdbc-javadoc.noarch             9.2.1002-5.el7              local    
postgresql-libs.i686                       9.2.18-1.el7                local    
postgresql-libs.x86_64                     9.2.18-1.el7                local    
postgresql-odbc.x86_64                     09.03.0100-2.el7            local    
postgresql-plperl.x86_64                   9.2.18-1.el7                local    
postgresql-plpython.x86_64                 9.2.18-1.el7                local    
postgresql-pltcl.x86_64                    9.2.18-1.el7                local    
postgresql-server.x86_64                   9.2.18-1.el7                local    
postgresql-test.x86_64                     9.2.18-1.el7                local    
postgresql-upgrade.x86_64                  9.2.18-1.el7                local    
qt-postgresql.i686                         1:4.8.5-13.el7              local    
qt-postgresql.x86_64                       1:4.8.5-13.el7              local    
qt5-qtbase-postgresql.i686                 5.6.1-10.el7                local    
qt5-qtbase-postgresql.x86_64               5.6.1-10.el7                local

2.2 rpm包安裝PostgreSQL

[root@localhost ~]# yum install -y postgresql-server postgresql postgresql-libs

server端:postgresql-server
client端:postgresql
依賴包:  postgresql-libs

1. 安裝完成後不能直接啟動數據庫,需要先手動初始化數據庫:

[root@localhost ~]# service postgresql initdb
Hint: the preferred way to do this is now "postgresql-setup initdb"
Initializing database ... OK

2. 再啟動數據庫:

[root@localhost ~]# systemctl start postgresql

[root@localhost ~]# systemctl status postgresql
● postgresql.service - PostgreSQL database server
  Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
  Active: active (running) since 一 2018-02-19 22:02:57 CST; 3min 37s ago
 Process: 1286 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=0/SUCCESS)
 Process: 1281 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 1290 (postgres)
   CGroup: /system.slice/postgresql.service
           ├─1290 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432
           ├─1291 postgres: logger process   
           ├─1293 postgres: checkpointer process   
           ├─1294 postgres: writer process   
           ├─1295 postgres: wal writer process   
           ├─1296 postgres: autovacuum launcher process   
           ├─1297 postgres: stats collector process   
           └─1391 postgres: postgres postgres [local] idle

2月 19 22:02:56 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
2月 19 22:02:57 localhost.localdomain systemd[1]: Started PostgreSQL database server.

3. 切換到操作系統下的“postgres”用戶,登陸數據庫:

[root@localhost ~]# su - postgres
-bash-4.2$ psql
psql (9.2.18)
輸入 "help" 來獲取幫助信息.

postgres=# \l
                                     資料庫列表
   名稱    |  擁有者  | 字元編碼 |  校對規則   |    Ctype    |       存取權限        
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 行記錄)

postgres=#

4. rpm包安裝的PostgreSQL的數據目錄,默認在/var/lib/pgsql/data

-bash-4.2$ ls -l /var/lib/pgsql/data/
總用量 48
drwx------ 5 postgres postgres    41 2月  19 22:01 base
drwx------ 2 postgres postgres  4096 2月  19 22:02 global
drwx------ 2 postgres postgres    18 2月  19 22:01 pg_clog
-rw------- 1 postgres postgres  4232 2月  19 22:01 pg_hba.conf
-rw------- 1 postgres postgres  1636 2月  19 22:01 pg_ident.conf
drwx------ 2 postgres postgres    32 2月  19 22:02 pg_log
drwx------ 4 postgres postgres    36 2月  19 22:01 pg_multixact
drwx------ 2 postgres postgres    18 2月  19 22:02 pg_notify
drwx------ 2 postgres postgres     6 2月  19 22:01 pg_serial
drwx------ 2 postgres postgres     6 2月  19 22:01 pg_snapshots
drwx------ 2 postgres postgres    25 2月  19 22:08 pg_stat_tmp
drwx------ 2 postgres postgres    18 2月  19 22:01 pg_subtrans
drwx------ 2 postgres postgres     6 2月  19 22:01 pg_tblspc
drwx------ 2 postgres postgres     6 2月  19 22:01 pg_twophase
-rw------- 1 postgres postgres     4 2月  19 22:01 PG_VERSION
drwx------ 3 postgres postgres    60 2月  19 22:01 pg_xlog
-rw------- 1 postgres postgres 19816 2月  19 22:01 postgresql.conf
-rw------- 1 postgres postgres    57 2月  19 22:02 postmaster.opts
-rw------- 1 postgres postgres    91 2月  19 22:02 postmaster.pid
rpm包的postgresql10安裝、初始化數據庫以及啟動方法(centos-7):

https://www.postgresql.org/download/linux/redhat/

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

yum install postgresql10

yum install postgresql10-server

/usr/pgsql-10/bin/postgresql-10-setup initdb
systemctl enable postgresql-10
systemctl start postgresql-10

2.3 源碼包安裝PostgreSQL

  1. 下載源代碼

[root@localhost ~]# wget https://ftp.postgresql.org/pub/source/v10.2/postgresql-10.2.tar.gz
  1. 安裝依賴包

[root@localhost ~]# yum install -y zlib-devel readline-devel gcc
  1. 編譯安裝PostgreSQL

[root@localhost ~]# tar -zxvf postgresql-10.2.tar.gz
[root@localhost ~]# cd postgresql-10.2

[root@localhost postgresql-10.2]# ./configure --prefix=/usr/local/pgsql10.2 --with-python --with-perl
[root@localhost postgresql-10.2]# make && make install

[root@localhost postgresql-10.2]# cd /usr/local/
[root@localhost local]# ln -s pgsql10.2 pgsql
備註:

??在PostgreSQL8.X中,編譯命令裏需要有“--enable-thread-safety”選項,而在PostgreSQL9.X以後的版本中不需要這個選項。
??因為在日常使用中,一般要求客戶端是線程安全的,PostgreSQL9.X以後的版本中考慮到這個問題,默認線程是安全的了。

??--with-perl:加上這個選項,才能使用perl語言的PL/Perl過程語言寫自定義函數,一般都需要。要使用這個選項需要先安裝perl-ExtUtils-Embed和perl-devel。
??--with-python:加上這個選項,才能使用perl語言的PL/Python過程語言寫自定義函數,一般都需要。要使用這個選項需要先安裝python-devel。
??按照官方文檔要求,使用make命令時,make的版本要在gmake3.8以上,目前大多數Linux發行版都滿足要求。(檢查方法:make --version)
??不指定--prefix選項,默認路徑將是/usr/local

異常處理:
編譯時增加 --with-python
configure: error: header file <Python.h> is required for Python
解決方法:
yum install python-devel
編譯時增加 --with-perl
configure: error: could not determine flags for linking embedded Perl.
This probably means that ExtUtils::Embed or ExtUtils::MakeMaker is notinstalled.
解決方法:
yum install perl-ExtUtils-Embed

4. 配置PostgreSQL環境變量

[root@localhost local]# vim /etc/profile
# PostgreSQL可執行文件路徑
export PATH=/usr/local/pgsql/bin:$PATH
# PostgreSQL共享庫路徑
export LD_LIBRARY_PATH=/usr/local/pgsql/lib

[root@localhost local]# source /etc/profile

5. 創建數據庫簇

[root@localhost local]# useradd postgres

[root@localhost local]# mkdir -pv /mydata/pgdata
mkdir: 已創建目錄 "/mydata"
mkdir: 已創建目錄 "/mydata/pgdata"

[root@localhost local]# chown -R postgres.postgres /mydata/pgdata/

[root@localhost local]# su - postgres
[postgres@localhost ~]$ /usr/local/pgsql/bin/initdb -D /mydata/pgdata/

6. 啟動PostgreSQL

[postgres@localhost pgsql]$ pg_ctl start -D /mydata/pgdata/
waiting for server to start....2018-02-20 00:52:03.616 CST [38915] LOG:  listening on IPv6 address "::1", port 5432
2018-02-20 00:52:03.616 CST [38915] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2018-02-20 00:52:03.619 CST [38915] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-02-20 00:52:03.689 CST [38916] LOG:  database system was shut down at 2018-02-20 00:44:56 CST
2018-02-20 00:52:03.692 CST [38915] LOG:  database system is ready to accept connections
 doneserver started

7. 停止PostgreSQL

[postgres@localhost pgsql]$ pg_ctl stop -D /mydata/pgdata/備註:
    可以在命令後增加 [-m SHUTDOWN-MODE],用來控制數據庫的停止方法;SHUTDOWN-MODE有以下三種:
        smart:等所有的連接終止後,關閉數據庫。如果客戶端連接不終止,則無法關閉數據庫。
        fast (PostgreSQL10.X默認):快速關閉數據庫,斷開客戶端的連接,讓已有的事物回滾,然後正常關閉數據庫。
                                   相當於Oracle數據庫關閉時的immediate(adj. 立即的)模式。
        immediate:不完整的關閉數據庫,相當於kill數據庫進程,下次啟動數據庫需要進行恢復。相當於Oracle數據庫關閉時的abort模式。
    其中,比較常用的是fast模式。

8. PostgreSQL啟動腳本 要使用service命令啟動PostgreSQL,需要把PostgreSQL的啟動腳本拷貝到 /etc/init.d/ 下,並增加執行權限

[root@localhost start-scripts]# pwd/root/postgresql-10.2/contrib/start-scripts

[root@localhost start-scripts]# lsfreebsd  linux  macos  osx

[root@localhost start-scripts]# cp linux /etc/init.d/postgresql
[root@localhost ~]# cd /etc/init.d
[root@localhost init.d]# chmod 755 postgresql

需要修改啟動腳本中的幾個參數:

[root@localhost init.d]# vim postgresql 
prefix=/usr/local/pgsql     # 確保可以通過這個目錄訪問到源碼安裝的PostgereSQL
PGDATA="/mydata/pgdata"     # 指向第5步創建的數據庫簇

備註:

??在centos7中,源碼安裝的PostgreSQL沒有提供systemctl需要的相關腳本,可以繼續使用service和chkconfig去管理。

2.4 安裝crontrib目錄下的工具

crontrib下面有一些工具比較實用,可以裝上,方法如下:

[root@localhost postgresql-10.2]# pwd/root/postgresql-10.2
[root@localhost postgresql-10.2]# cd contrib/
[root@localhost contrib]# make && make install

安裝後 /usr/local/pgsql/bin 會增加三個命令:oid2name pg_standby vacuumlo

2.5 使用較大的數據塊提高I/O性能

??如果希望使用較大的數據塊提高I/O性能,只能通過源碼編譯安裝解決解決,在執行 ./configure 命令時指定較大數據塊,同時也需要指定較大的WAL日誌塊和WAL日誌文件的大小。
??如果想指定32KB數據塊、32KB的WAL日誌塊、64MB的WAL日誌文件,./configure 的命令如下:

./configure --prefix=/usr/local/pgsql10.2 --with-python --with-perl --with-blocksize=32 --with-wal-blocksize=32 --with-wal-segsize=64
備註:
    --with-blocksize        的範圍在1-32KB,默認8KB
    --with-wal-blocksize    的範圍在1-64KB,默認8KB
    --with-wal-segsize      的範圍在1-1024MB,默認16MB    
註意:
    修改數據塊後的PostgreSQL創建的PostgreSQL數據庫,不能使用其它塊大小的PostgreSQL程序啟動。

2.6 PostgreSQL簡單配置

2.6.1 修改PostgreSQL監聽IP和端口

監聽IP和端口需要通過數據目錄下的postgresql.conf文件修改:

[root@localhost pgdata]# pwd
/mydata/pgdata
[root@localhost pgdata]# vim postgresql.conf
#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)

??監聽IP使用localhost時,只能通過127.0.0.1訪問數據庫;
??如果需要通過其他網絡遠程訪問PostgreSQL,可以使用“,”作為分隔符,把IP地址添加到listen_addresses後,或者使用“*”,讓所有IP都可以訪問數據庫。

2.6.2 配置數據庫log

1. 日誌開關以及保存目錄名

logging_collector = on      # 日誌的開關,默認是off,不會收集日誌
log_directory = 'log'       # 日誌的保存目錄名,默認是在數據目錄下的log目錄裏,使用默認值即可,不需要修改

2. 日誌的輪轉配置 可以使用以下幾種方案:

  • 每天生成一個新的日誌

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'     #日誌文件名
log_truncate_on_rotation = off      # 同名日誌覆蓋開關
log_rotation_age = 1d       # 按時間輪轉日誌
log_rotation_size = 0       # 按日誌大小輪轉日誌
  • 每當日誌寫滿一定大小(如10MB),進行一次日誌輪轉

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = offlog_rotation_age = 0
log_rotation_size = 10M
  • 只保留7天的日誌,進行循環覆蓋

log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
log_rotation_age = 1dlog_rotation_size = 0

說明:
??%a 代表星期,例如:在星期二時,就會生成postgresql-Tue.log這個日誌;
??每當遇到同名的日誌時,會對舊的日誌文件進行覆蓋,而不是在舊的日誌文件上進行追加。

2.6.3 內存參數配置

PostgreSQL安裝完成後,可以主要修改以下兩個主要內存參數:

  • shared_buffer:共享內存的大小,主要用於共享數據塊,默認是128MB;
    如果服務器內存有富余,可以把這個參數適當改大一些,這樣數據庫就可以緩存更多的數據塊,當讀取數據時,就可以從共享內存中讀取,而不需要去文件讀取。

  • work_mem:單個SQL執行時,排序、hash join所使用的內存,SQL運行完成後,內存就釋放了,默認是4MB;
    增加這個參數,可以提高排序操作的速度。


1. PostgreSQL-安裝和基本配置(學習筆記)