根據(jù)業(yè)務(wù)需求選擇版本,官網(wǎng)下載
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm yum install postgresql96 postgresql96-server rpm -qa|grep postgre
執(zhí)行完初始化任務(wù)之后,postgresql 會(huì)自動(dòng)創(chuàng)建和生成兩個(gè)用戶
和一個(gè)數(shù)據(jù)庫(kù)
:
系統(tǒng)用戶
postgres:管理數(shù)據(jù)庫(kù)的系統(tǒng)用戶;$passwd postgres
數(shù)據(jù)庫(kù)用戶
postgres:數(shù)據(jù)庫(kù)超級(jí)管理員此默認(rèn)數(shù)據(jù)庫(kù)
為postgres/usr/pgsql-9.6/bin/postgresql96-setup initdb
設(shè)置成 centos7 開機(jī)啟動(dòng)服務(wù)
systemctl enable postgresql-9.6
啟動(dòng) postgresql 服務(wù)
systemctl start postgresql-9.6 systemctl status postgresql-9.6
pgsql9.6配置文件位置默認(rèn)在:/var/lib/pgsql/9.6/data/postgresql.conf
監(jiān)聽I(yíng)P使用localhost時(shí),只能通過127.0.0.1訪問數(shù)據(jù)庫(kù);
如果需要通過其他遠(yuǎn)程地址訪問PostgreSQL,可以使用“,”作為分隔符,把IP地址添加到listen_addresses后,或者使用“*”,讓所有IP都可以訪問數(shù)據(jù)庫(kù)。
注意:這里只是開啟數(shù)據(jù)庫(kù)的遠(yuǎn)程訪問
權(quán)限,具體是否能夠進(jìn)行遠(yuǎn)程登錄
,還需要依據(jù)pg_hba.conf
的認(rèn)證配置,詳細(xì)內(nèi)容見下節(jié)。
# - 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)
日志收集,一般是打開的
# This is used when logging to stderr: logging_collector = on # Enable capturing of stderr and csvlog # into log files. Required to be on for # csvlogs. # (change requires restart)
日志目錄,一般使用默認(rèn)值
# These are only used if logging_collector is on: log_directory = 'pg_log' # directory where log files are written, # can be absolute or relative to PGDATA
只保留一天的日志,進(jìn)行循環(huán)覆蓋
log_filename = 'postgresql-%a.log' # log file name pattern, # can include strftime() escapes log_truncate_on_rotation = on # If on, an existing log file of the # same name as the new log file will be # truncated rather than appended to. # But such truncation only occurs on # time-driven rotation, not on restarts # or size-driven rotation. Default is # off, meaning append to existing files # in all cases. log_rotation_age = 1d # Automatic rotation of logfiles will # happen after that time. 0 disables. log_rotation_size = 0 # Automatic rotation of logfiles will
共享內(nèi)存的大小,用于共享數(shù)據(jù)塊。如果你的機(jī)器上有足夠的內(nèi)存,可以把這個(gè)參數(shù)改的大一些,這樣數(shù)據(jù)庫(kù)就可以緩存更多的數(shù)據(jù)塊,當(dāng)讀取數(shù)據(jù)時(shí),就可以從共享內(nèi)存中讀,而不需要再?gòu)奈募先プx取。
# - Memory - shared_buffers = 32MB # min 128kB # (change requires restart)
單個(gè)SQL執(zhí)行時(shí),排序、hash json所用的內(nèi)存,SQL運(yùn)行完后,內(nèi)存就釋放了。
# actively intend to use prepared transactions. #work_mem = 1MB # min 64kB
PostgreSQL安裝完成后,可以主要修改以下兩個(gè)主要內(nèi)存參數(shù):
shared_buffer:共享內(nèi)存的大小,主要用于共享數(shù)據(jù)塊,默認(rèn)是128MB;
如果服務(wù)器內(nèi)存有富余,可以把這個(gè)參數(shù)適當(dāng)改大一些,這樣數(shù)據(jù)庫(kù)就可以緩存更多的數(shù)據(jù)塊,當(dāng)讀取數(shù)據(jù)時(shí),就可以從共享內(nèi)存中讀取,而不需要去文件讀取。
work_mem:?jiǎn)蝹€(gè)SQL執(zhí)行時(shí),排序、hash join所使用的內(nèi)存,SQL運(yùn)行完成后,內(nèi)存就釋放了,默認(rèn)是4MB;
增加這個(gè)參數(shù),可以提高排序操作的速度。
如果想連接到數(shù)據(jù)庫(kù),需要切換到postgres用戶下(默認(rèn)的認(rèn)證配置前提下)
在postgres用戶下連接數(shù)據(jù)庫(kù),是不需要密碼的。
切換 postgres 用戶后,提示符變成 -bash-4.2$
使用psql連接到數(shù)據(jù)庫(kù)控制臺(tái),此時(shí)系統(tǒng)提示符變?yōu)?postgres=#'
$ su postgres bash-4.2$ psql psql (9.6) Type "help" for help. postgres=#
命令 | 作用 |
---|---|
\h | 查看所有sql命令,\h select 等可以查看具體命令 |
? | 查看所有psql命令 |
\d | 查看當(dāng)前數(shù)據(jù)庫(kù)所有表 |
\d | [tablename] 查看具體的表結(jié)構(gòu) |
\du | 查看所有用戶 |
\l | 查看所有數(shù)據(jù)庫(kù) |
\e | 打開文本編輯器 |
數(shù)據(jù)庫(kù)創(chuàng)建與修改
# 創(chuàng)建數(shù)據(jù)庫(kù) create database testdb; # 刪除數(shù)據(jù)庫(kù) drop database testdb; # 重命名數(shù)據(jù)庫(kù)(該數(shù)據(jù)庫(kù)必須沒有活動(dòng)的連接) alter database testdb rename to newname; # 以其他數(shù)據(jù)庫(kù)為模板創(chuàng)建數(shù)據(jù)庫(kù)(表結(jié)構(gòu)、數(shù)據(jù)都會(huì)復(fù)制) create database newdb template testdb; # 將查詢結(jié)果寫入文件 \o /tmp/test.txt select * from test; # 列狀顯示 \w # 再一次\o關(guān)閉寫入,否則是連續(xù)寫入的 \o # 退出控制臺(tái) \q
數(shù)據(jù)庫(kù)用戶創(chuàng)建與授權(quán)
# 建立新的數(shù)據(jù)庫(kù)用戶 create user zhangsan with password '123456'; # 為新用戶建立數(shù)據(jù)庫(kù) create database testdb owner zhangsan; # 把新建的數(shù)據(jù)庫(kù)權(quán)限賦予新用戶 grant all privileges on database testdb to zhangsan;
認(rèn)證權(quán)限配置文件: /var/lib/pgsql/9.6/data/pg_hba.conf
命令行的各個(gè)參數(shù)解釋說明:
常見的四種身份驗(yàn)證方式
postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
pg_hba.conf
所有的用戶通過任意ip都可以通過md5(密碼)的方式登陸PostgreSQL,配置如下:
host all all 0.0.0.0/0 ident
驗(yàn)證
# server:重啟生效 systemctl restart postgresql-9.6 # client:命令行遠(yuǎn)程登錄 psql -U zhangsan -d testdb -h 10.122.45.97 -p 5432
PostgreSQL登陸默認(rèn)是peer,不需要驗(yàn)證用戶密碼即可進(jìn)入psql相關(guān)數(shù)據(jù)庫(kù),但前提是必須切換用戶登陸。類似于最開始執(zhí)行的su postgres;psql
一樣。
[root@sltkp3cbpch data]# psql -U zhangsan -d testdb -p 5432 psql: FATAL: Peer authentication failed for user "zhangsan"
如果必須按照上述登陸方式登陸的話,有兩種修改方式:
a. map映射
map
映射是用來(lái)將系統(tǒng)用戶映射到對(duì)應(yīng)的postgres數(shù)據(jù)庫(kù)用戶,用來(lái)限制指定的用戶使用指定的賬號(hào)來(lái)登陸。
pg_ident.conf
修改pg_ident.conf文件,與pg_hba.conf文件同級(jí)目錄。其基本格式如下:
# MAPNAME SYSTEM-USERNAME PG-USERNAME map_zhangsan root zhangsan
map_zhangsan
root
zhangsan
上面定義的map
意思是:定義了一個(gè)叫做map_zhangsan
的映射,當(dāng)客戶端用戶是root
的時(shí)候,允許它用zhangsan
用戶來(lái)登陸PostgreSQL。
修改pg_hba.conf文件
在peer的認(rèn)證方式后面添加:map=map_tom
重啟PostgreSQL服務(wù),再次嘗試,連接成功。
b. 修改認(rèn)證方式
需要修改一下pg_hba.cong
文件,將local all all peer
修改為local all all md5
,如下圖所示:
重啟PostgreSQL服務(wù),再次嘗試,連接成功。
到此這篇關(guān)于postgresql安裝及配置超詳細(xì)教程的文章就介紹到這了,更多相關(guān)postgresql安裝及配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:烏海 錦州 蚌埠 來(lái)賓 珠海 晉城 株洲 衡陽(yáng)
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《postgresql安裝及配置超詳細(xì)教程》,本文關(guān)鍵詞 postgresql,安裝,及,配置,超,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。