一、編譯安裝:
- 環境准備:
- GNU make 版本 >=3.8 (make --version)
- ISO/ANSI C 編譯器,至少須兼容 C89 標准,GCC 或 intel 編譯器等均可
- gzip 、bzip2 、tar ,源碼解包
- GNU Readline 庫,用於 psql 操作歷史命令
- 安全相關模塊:Kerberos、OpenSSL、OpenLDAP、PAM 等,需要事先安裝好對應的包
- 如果要使用 PL/Python、PL/Perl、PL/Tcl 等過程語言,須保証語言本身及相關的開發庫安裝到位
- 如果使用的是 Git 中的源碼或需要做服務器端開發,則另外需要 GNU Flex(>=2.5.31) 及 Bison(>=1.875),不能使用其它 lex 或 yacc 程序
- 磁盤空間:編譯過程源碼樹占用 100M+,生成的程序文件 20M+,一個空數據庫 data 目錄 30M+,回歸測試空間 150M+;注:一個數據庫所占的空間大約是存儲同等數據的平面文件所占空間的五倍
- configure 編譯選項:
- --prefix=PREFIX 指定安裝目錄,默認為 /usr/local/pgsql
- --bindir= 可執行文件目錄,默認 /PREFIX/bin
- --sysconfdir= 配置文件目錄,默認 /PREFIX/etc
- --libdir= 庫文件目錄,默認 /PREFIX/lib
- --includedir= 頭文件目錄,默認 /PREFIX/include
- --datarootdir=DATAROOTDIR 設置只讀共享文件目錄,默認為 /PREFIX/share
- --mandir= man手冊目錄,默認 /DATAROOTDIR/man
- --with-extra-version=STRING 在版本號后面追加 STRING 字符串,用作自定義版本標識
- --with-pgport=NUMBER 指定服務器端與客戶端的默認端口號,缺省為 5432
- --with-openssl 編譯 ssl 支持,需要事先安裝 OpenSSL 包
- --with-pam 編譯 PAM 支持
- --with-ldap 編譯 LDAP 支持,需要事先安裝 OpenLDAP 包
- --with-segsize= 設定 segment size ,以 gigabytes(GB) 為單位,默認為 1GB ,大型的表會按 segsize 被分割成多個文件
- --with-blocksize= 指定塊大小,是表內存儲和 I/O 的基本單位,默認 8Kbytes ,通常無需變更,取值范圍 1-32KB
- --with-wal-segsize= 設置 WAL(Write-Ahead Logging) 的 segment size ,以 megabytes(MB) 為單位,默認 16MB ,取值范圍 1-64MB
- --with-wal-blocksize= 指定 WAL 的塊大小,這是 WAL 預寫式日志存儲和 I/O 的基本單位,以 Kbytes 為單位,默認 8KB ,取值范圍 1-64KB
- --enable-debug (Compiles all programs and libraries with debugging symbols. This means that you can run the programs in a debugger to analyze problems),使用 GCC 編譯器時可用於生產環境,其它編譯器會影響性能;多用於開發
- --enable-profiling GCC編譯器下,所有程序和庫將被編譯成可進行性能分析,后端退出時生成的 gmon.out 用於性能分析;多用於開發
- --enable-coverage 代碼覆蓋率測試,僅用於開發
- --enable-cassert 開啟服務器的 assertion(斷言) 檢查,僅用於開發
- --enable-depend (Enables automatic dependency tracking. With this option, the makefiles are set up so that all affected object files will be rebuilt when any header file is changed),僅用於開發
- --enable-dtrace 編譯動態追蹤工具 Dtrace 支持,當前尚不能用於 Linux 平台,可用於 FreeBSD、Solaris
- --with-systemd 開啟 systemd 支持,需9.6及以上版本
- configure 環境變量,可以在命令行上指定環境變量,形如: ./configure CC=/opt/bin/gcc CFLAGS='-O2 -pipe'
- CC 指定 C 編譯器
- CFLAGS 設置 C 編譯器選項
- CPP 指定 C 預編譯器
- CPPFLAGS 設置 C 預編譯器選項
- make 選項:
- make 按默認配置編譯
- make world 同時編譯文檔及附加模塊(contrib)
- make check 回歸測試,用於安裝完成后測試功能完整性,不能以 root 身份運行
- make install 安裝程序
- make install-docs 安裝文檔(info、man手冊)
- make install-world 安裝所有可安裝的
- make uninstall 刪除安裝的文件(生成的目錄無法刪除)
- make clean 清除 make 生成的文件,但保留 configure 生成的文件
- make distclean 將源碼恢復為原始狀態,即同時刪除 make 與 configure 階段生成的文件
- make -C 指定源文件目錄,僅安裝程序的特定部分;如只安裝客戶端應用和接口:make -C src/bin install; make -C src/include install; make -C src/interfaces install; make -C doc install
二、安裝后設置
- 共享庫
- 法一:在 /etc/profile 或 ~/.bash_profile 中寫入:export LD_LIBRARY_PATH=/usr/local/pgsql/lib ,目標須目錄與安裝時指定的一致
- 法二:在 /etc/ld.so.conf.d/ 目錄下創建 pgsql.conf 或直接操作 /etc/ld.so.conf 文件,寫入 /usr/local/pgsql/lib ,之后運行 ldconfig 更新緩存
- 環境變量
- 在 /etc/profile 或 ~/.bash_profile 中寫入:
- export PATH=/usr/local/pgsql/bin:$PATH
- export MANPATH=/usr/local/pgsql/man:$MANPATH
- 變量 PGHOST 與 PGPORT 用於為客戶端應用指定數據庫服務器的主機和端口,它會覆蓋編譯時的默認項
- 在 /etc/profile 或 ~/.bash_profile 中寫入:
三、服務器設置和操作
- PostgreSQL 用戶賬戶
- 建議性原則:新建一個獨立的私有賬戶,專用於運行 PostgreSQL 服務器端,作為數據庫 data 目錄及其父目錄的屬主,但不能把可執行文件安裝為屬於這個用戶
- useradd 添加系統用戶,createuser 創建對應的 PostgreSQL 同名用戶,createdb 創建數據庫
- Creating a Database Cluster(catalog cluster)
- initdb -D /usr/local/pgsql/data
- 或 設置 PGDATA環境變量后直接執行 initdb 或 pg_ctl initdb
- 或 pg_ctl -D /usr/local/pgsql/data initdb
- 使用單獨的數據分區
- 不要將分區的項層目錄(掛載點)作為 Database Cluster 目錄,應在其上建立一個目錄(父目錄),然后在其中創建數據目錄,在避免出現權限問題的同時,可防止由於掛載點斷線造成的數據紊亂
- Starting the Database Server
- pg_ctl start -l logfile -D /PATH/TO/data 或 postgres -D /PATH/TO/data >serverlog 2>&1 & ,serverlog 須替換成實際的日志文件路徑
- 設置 PostgreSQL 開機啟動時,不能以 root 或其它用戶啟動,應在啟動腳本中使用類似:su postgres -c 'pg_ctl start -l serverlog -D /PATH/TO/data'
- 服務器運行時,PID 被保存在 data 目錄下的 postmaster.pid文件中的第一行
- 服務啟動常見問題
- 端口被占用(已有正在運行的 PostgreSQL 進程或其它程序)
- 試圖使用一個保留端口 (<1024),如:postgres -p 666
- 共享內存 (System-V-style shared memory) 或 信號量 (System V semaphores) 不足,能常是由於內核限制或根本沒有開啟對應的支持項 (If you get an "illegal system call" error,it is likely that shared memory or semaphores are not supported in your kernel at all)
- 客戶端連接常見問題
- 服務器沒有啟動或沒有配置成允許 TCP/IP 連接,通常會收到 'psql: could not connect to server: ...' 類似的錯誤提示
- 客戶端自身網絡問題等
- 共享內存 (shm) 與信號量 (sem)
- 修改 /etc/sysctl.conf 中的 kernel.shmmax 等,立即生效需執行 sysctl -p
- 或 更改 /proc/sys/kernel/shmall 等
- 系統資源限制 (/proc/sys/fs/file-max 等)
- the number of processes per user
- the number of open files per process
- the amount of memory available to each process
- 應對 out-of-memory (OOM) killer
- Pid=`head -n 1 /PATH/TO/postmaster.pid` && echo -1000 > /proc/$Pid/oom_score_adj 首選
- 或 Pid=`pidof postgres` && echo -17 > /proc/$Pid/oom_adj 通常用於 2.6 版本以前的內核
- 內核源碼參見:...src/linux/include/uapi/linux/oom.h
- 使用 Linux 大頁內存
- 更改 /proc/sys/vm/nr_hugepages 的值
- 或 設定 /etc/sysctl.conf 中 vm.nr_hugepages=
- 關閉服務器
- 通常使用 SIGTERM(等待所有進程正常關閉后退出,包括備份) 或 SIGINT(與 SIGTERM 類同,但不會等待備份進程) 信號,不推薦使用 SIGQUIT(5 秒種內沒有正常退出的進程將被直接關閉) 和 SIGKILL(立即無條件退出,此信號不能同時關閉子進程,且來不及釋放共享內存與信號量)
- 如:kill -INT `head -1 /PATH/TO/postmaster.pid`
- 注:可以指定的給子進程發送信號
- Upgrading a PostgreSQL Cluster
- 對於次版本號之間的升級,直接更新可執行文件並重啟服務器即可
- The traditional method for moving data to a new major version is to dump and reload the database, though this can be slow
- pg_dumpall > outputfile
- pg_ctl stop
- mv /usr/local/pgsql /usr/local/pgsql.old 備份舊版本可執行文件
- Install the new version of PostgreSQL
- Create a new database cluster if needed:/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
- 配置新服務器的 pg_hba.conf 與 postgresql.conf 等
- Start the new database server:pg_ctl start -D /usr/local/pgsql/data
- Finally, restore your data from backup with:/usr/local/pgsql/bin/psql -f outputfile
- 通過 pg_upgrade 升級:https://www.postgresql.org/docs/9.6/static/pgupgrade.html
- pg_upgrade -b oldbindir -B newbindir -d olddatadir -D newdatadir [option...]
- ...
四、Encryption and Secure
- Preventing Server Spoofing
- OpenSSL
- SSH tunneling