PostgreSQL數據庫是目前功能最強大的開源數據庫,支持豐富的數據類型(如JSON和JSONB類型,數組類型)和自定義類型。並且它提供了豐富的接口,能夠很容易地擴展它的功能,如能夠在GiST框架下實現本身的索引類型等,它還支持使用C語言寫自定義函數、觸發器,也支持使用流行的語言寫自定義函數、好比其中的PL/Perl提供了使用Perl語言寫自定義函數的功能,固然還有PL/Python,PL/Tcl,等等。html
PostgreSQL數據庫是目前功能最強大的開源數據庫,它是最接近工業標準SQL92的查詢語言,而且正在實現新的功能以兼容最新的SQL標準:SQL2003。java
穩定可靠:PostgreSQL是惟一可以作到數據零丟失的開源數據庫。有報道稱國外部分銀行也在使用PostgreSQL數據庫。python
開源省錢:PostgreSQL數據庫是開源的、免費的,並且是BSD協議,在使用和二次開發上基本沒有限制。linux
支持普遍:PostgreSQL數據庫支持大量的主流開發語言,包括C、C++、Perl、Python、Java、Tcl,以及PHP等。sql
PostgreSQL社區活躍:PostgreSQL基本上每三個月推出一個補丁版本,這意味着已知的BUG很快會被修復,有應用場景的需求也會及時獲得響應。數據庫
[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
[root@localhost ~]# yum install -y postgresql-server postgresql postgresql-libs server端:postgresql-server client端:postgresql 依賴包: postgresql-libs
1. 安裝完成後不能直接啓動數據庫,須要先手動初始化數據庫:macos
[root@localhost ~]# service postgresql initdb Hint: the preferred way to do this is now "postgresql-setup initdb" Initializing database ... OK
2. 再啓動數據庫:vim
[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」用戶,登錄數據庫:centos
[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
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
下載源代碼
[root@localhost ~]# wget https://ftp.postgresql.org/pub/source/v10.2/postgresql-10.2.tar.gz
安裝依賴包
[root@localhost ~]# yum install -y zlib-devel readline-devel gcc
編譯安裝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去管理。
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
若是但願使用較大的數據塊提升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程序啓動。
監聽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均可以訪問數據庫。
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這個日誌;
每當遇到同名的日誌時,會對舊的日誌文件進行覆蓋,而不是在舊的日誌文件上進行追加。
PostgreSQL安裝完成後,能夠主要修改如下兩個主要內存參數:
shared_buffer:共享內存的大小,主要用於共享數據塊,默認是128MB;
若是服務器內存有富餘,能夠把這個參數適當改大一些,這樣數據庫就能夠緩存更多的數據塊,當讀取數據時,就能夠從共享內存中讀取,而不須要去文件讀取。
work_mem:單個SQL執行時,排序、hash join所使用的內存,SQL運行完成後,內存就釋放了,默認是4MB;增長這個參數,能夠提升排序操做的速度。