官方文檔
二進(jìn)制安裝和啟動(dòng):
https: //www.postgresql.org /download/linux/redhat/
源碼安裝和啟動(dòng):
https:/ /www.postgresql.org/docs/11/install-procedure.html
https:/ /www.postgresql.org/docs/11/creating-cluster.html
https:/ /www.postgresql.org/docs/11/server-start.html
https:/ /www.postgresql.org/docs/current/kernel-resources.html
兩種方法
1、二進(jìn)制安裝(linux是rpm包,windows是exe安裝文件)
2、源碼安裝,推薦安裝方式
二進(jìn)制安裝
示例:centos7_64平臺(tái),數(shù)據(jù)庫(kù)postgresql11,使用yum安裝
不使用yum的話可以直接使用rpm包安裝,rpm包下載地址https://yum.postgresql.org/rpmchart.php
1、安裝RPM的yum源,其實(shí)就是下載一個(gè)postgresql的yum源pgdg-redhat-all.repo文件到/etc/yum.repos.d目錄下,有了這個(gè)yum源后,就可以直接yum install postgresql11安裝postgresql數(shù)據(jù)庫(kù)了
yum install
https: / /download.postgresql.org/pub/repos/yum/reporpms/EL- 7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2、安裝客戶端,先執(zhí)行search看有哪些PostgreSQL client可供安裝
yumsearch 'PostgreSQL client'
yum install postgresql11
3、安裝服務(wù)端,先執(zhí)行search看有哪些PostgreSQL server可供安裝,以下步驟安裝好后,會(huì)自動(dòng)創(chuàng)建用戶postgres,自動(dòng)創(chuàng)建目錄/usr/pgsql-11
yumsearch 'PostgreSQL server'
yum install postgresql11-server
4、初始化數(shù)據(jù)庫(kù)并啟用開機(jī)自動(dòng)啟動(dòng)
/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl enablepostgresql-11
systemctl start postgresql-11
5、查看postgresql的進(jìn)程
[root@zabbixtest2 ~] # ps -ef|grep postgres
postgres 15271001: 13? 00: 00: 00/usr /pgsql-11/bin/postmaster -D /var/lib/pgsql/11/data/
6、連接postgresql數(shù)據(jù)庫(kù)
su- postgres
psql -l
psql -d postgres
源碼安裝
1、建立postgresql用戶、內(nèi)核資源的配置,主要涉及/etc/security/limits.conf、/etc/sysctl.conf文件,類似oracle也需要配置這兩個(gè)文件
2、解壓tar包,進(jìn)入解壓目錄,使用root用戶執(zhí)行如下,最后的make install命令把軟件安裝到/postgresql/pgsql目錄
./configure --prefix=/postgresql/pgsql
make
make install
3、把軟件安裝目錄/postgresql/pgsql授權(quán)宿主用戶為postgresql
4、編輯postgresql用戶的.bash_profile文件,PATH= /postgresql/pgsql/bin :$PATH、LD_LIBRARY_PATH= /postgresql/pgsql /lib
5、初始化數(shù)據(jù)庫(kù),后面兩條命令任意一條都可以
su - postgresql
initdb -D /postgresql/pgsql/ data
pg_ctl -D /postgresql/pgsq l/ datainitdb
6、啟動(dòng)postgresql程序,下面任意一個(gè)都可以,官方文檔建議使用pg_ctl
postgres -D /postgresql/pgsql/ data>logfile 2>& 1&
pg_ctl start -D /postgresql/pgsql/ data-l logfile
7、查看postgresql的進(jìn)程
[root@zabbixtest1 ~] # ps -ef|grep postgres
postgre+ 8031007: 10pts/ 000: 00: 00/postgresql/pgsql/bin/postgres -D /postgresql/pgsql/data
8、連接postgresql數(shù)據(jù)庫(kù),指定連接postgres庫(kù)
psql-l
psql -d postgres
備注 :psql命令不加端口和不加數(shù)據(jù)庫(kù)名,表示默認(rèn)進(jìn)入端口為5432并且數(shù)據(jù)庫(kù)名和初始數(shù)據(jù)庫(kù)initdb時(shí)的用戶名一樣的數(shù)據(jù)庫(kù),比如初始數(shù)據(jù)庫(kù)initdb時(shí)的用戶名為A,則默認(rèn)進(jìn)入A庫(kù)。postgresql的默認(rèn)端口是5432,默認(rèn)數(shù)據(jù)庫(kù)是postgres、template0、template1
如果此時(shí)端口不是5432,則會(huì)報(bào)錯(cuò)psql: FATAL: role "A" does not exist
因?yàn)榘惭b用戶"A"對(duì)應(yīng)的實(shí)例端口為其他,端口5432數(shù)據(jù)庫(kù)的Owner不是"A"
如果此時(shí)沒有"A"庫(kù),則會(huì)報(bào)錯(cuò)psql: FATAL: database "A" does not exist
因?yàn)榘惭b用戶"A",默認(rèn)進(jìn)入"A"庫(kù),而"A"庫(kù)是不存在的
9、創(chuàng)建一個(gè)名為test的數(shù)據(jù)庫(kù)
createdb test
10、查看數(shù)據(jù)庫(kù)狀態(tài)
pg_ctlstatus -D /postgresql/pgsql/data
11、關(guān)閉數(shù)據(jù)庫(kù)
pg_ctl stop-D /postgresql/pgsql/ data
官方文檔Short Version簡(jiǎn)要步驟
./configure
make
su
make install
adduser postgres
mkdir /usr/ local/pgsql/ data
chown postgres /usr/ local/pgsql/ data
su - postgres
/usr/ local/pgsql/ bin/initdb -D /usr/ local/pgsql/ data
/usr/ local/pgsql/ bin/postgres -D /usr/ local/pgsql/ data> logfile2>& 1&
/usr/ local/pgsql/ bin/createdb test
/usr/ local/pgsql/ bin/psql test
另:想了解更多數(shù)據(jù)庫(kù)的知識(shí)與用法,歡迎關(guān)注墨天輪 “PostgreSQL數(shù)據(jù)庫(kù)專欄”(地址:https://www.modb.pro/db,或者掃描下方二維碼可直達(dá)),此外,墨天輪開放了很多數(shù)據(jù)庫(kù)專欄,如 GaussDB、PolarDB、OceanBase、TDSQL、GoldenDB 等眾多數(shù)據(jù)庫(kù)專欄,歡迎關(guān)注學(xué)習(xí)!






