postgresql安裝及配置超詳細教程
1. 安裝
根據(jù)業(yè)務需求選擇版本,官網(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
初始化數(shù)據(jù)庫
執(zhí)行完初始化任務之后,postgresql 會自動創(chuàng)建和生成兩個用戶
和一個數(shù)據(jù)庫
:
- linux
系統(tǒng)用戶
postgres:管理數(shù)據(jù)庫的系統(tǒng)用戶; - 密碼由于是默認生成的,需要在系統(tǒng)中修改一下,
$passwd postgres
數(shù)據(jù)庫用戶
postgres:數(shù)據(jù)庫超級管理員此- 用戶
默認數(shù)據(jù)庫
為postgres
/usr/pgsql-9.6/bin/postgresql96-setup initdb
設置成 centos7 開機啟動服務
systemctl enable postgresql-9.6
啟動 postgresql 服務
systemctl start postgresql-9.6 systemctl status postgresql-9.6
2. PostgrepSQL的簡單配置
pgsql9.6配置文件位置默認在:/var/lib/pgsql/9.6/data/postgresql.conf
2.1 修改監(jiān)聽的ip和端口
監(jiān)聽IP使用localhost時,只能通過127.0.0.1訪問數(shù)據(jù)庫;
如果需要通過其他遠程地址訪問PostgreSQL,可以使用“,”作為分隔符,把IP地址添加到listen_addresses后,或者使用“*”,讓所有IP都可以訪問數(shù)據(jù)庫。
注意:這里只是開啟數(shù)據(jù)庫的遠程訪問
權限,具體是否能夠進行遠程登錄
,還需要依據(jù)pg_hba.conf
的認證配置,詳細內(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)
2.2 修改數(shù)據(jù)庫log相關的參數(shù)
日志收集,一般是打開的
# 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)
日志目錄,一般使用默認值
# 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
只保留一天的日志,進行循環(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
2.3 內(nèi)存參數(shù)
共享內(nèi)存的大小,用于共享數(shù)據(jù)塊。如果你的機器上有足夠的內(nèi)存,可以把這個參數(shù)改的大一些,這樣數(shù)據(jù)庫就可以緩存更多的數(shù)據(jù)塊,當讀取數(shù)據(jù)時,就可以從共享內(nèi)存中讀,而不需要再從文件上去讀取。
# - Memory - shared_buffers = 32MB # min 128kB # (change requires restart)
單個SQL執(zhí)行時,排序、hash json所用的內(nèi)存,SQL運行完后,內(nèi)存就釋放了。
# actively intend to use prepared transactions. #work_mem = 1MB # min 64kB
PostgreSQL安裝完成后,可以主要修改以下兩個主要內(nèi)存參數(shù):
shared_buffer:共享內(nèi)存的大小,主要用于共享數(shù)據(jù)塊,默認是128MB;
如果服務器內(nèi)存有富余,可以把這個參數(shù)適當改大一些,這樣數(shù)據(jù)庫就可以緩存更多的數(shù)據(jù)塊,當讀取數(shù)據(jù)時,就可以從共享內(nèi)存中讀取,而不需要去文件讀取。
work_mem:單個SQL執(zhí)行時,排序、hash join所使用的內(nèi)存,SQL運行完成后,內(nèi)存就釋放了,默認是4MB;
增加這個參數(shù),可以提高排序操作的速度。
3. 數(shù)據(jù)庫的基礎操作
3.1 連接數(shù)據(jù)庫控制臺
如果想連接到數(shù)據(jù)庫,需要切換到postgres用戶下(默認的認證配置前提下)
在postgres用戶下連接數(shù)據(jù)庫,是不需要密碼的。
切換 postgres 用戶后,提示符變成 -bash-4.2$
使用psql連接到數(shù)據(jù)庫控制臺,此時系統(tǒng)提示符變?yōu)?postgres=#'
$ su postgres bash-4.2$ psql psql (9.6) Type "help" for help. postgres=#
3.2 一些常用控制臺命令
命令 | 作用 |
---|---|
\h | 查看所有sql命令,\h select 等可以查看具體命令 |
? | 查看所有psql命令 |
\d | 查看當前數(shù)據(jù)庫所有表 |
\d | [tablename] 查看具體的表結(jié)構 |
\du | 查看所有用戶 |
\l | 查看所有數(shù)據(jù)庫 |
\e | 打開文本編輯器 |
3.3 SQL控制臺操作語句
數(shù)據(jù)庫創(chuàng)建與修改
# 創(chuàng)建數(shù)據(jù)庫 create database testdb; # 刪除數(shù)據(jù)庫 drop database testdb; # 重命名數(shù)據(jù)庫(該數(shù)據(jù)庫必須沒有活動的連接) alter database testdb rename to newname; # 以其他數(shù)據(jù)庫為模板創(chuàng)建數(shù)據(jù)庫(表結(jié)構、數(shù)據(jù)都會復制) create database newdb template testdb; # 將查詢結(jié)果寫入文件 \o /tmp/test.txt select * from test; # 列狀顯示 \w # 再一次\o關閉寫入,否則是連續(xù)寫入的 \o # 退出控制臺 \q
數(shù)據(jù)庫用戶創(chuàng)建與授權
# 建立新的數(shù)據(jù)庫用戶 create user zhangsan with password '123456'; # 為新用戶建立數(shù)據(jù)庫 create database testdb owner zhangsan; # 把新建的數(shù)據(jù)庫權限賦予新用戶 grant all privileges on database testdb to zhangsan;
4. 認證登錄
認證權限配置文件: /var/lib/pgsql/9.6/data/pg_hba.conf
命令行的各個參數(shù)解釋說明:
- -U username 用戶名,默認值postgres
- -d dbname 要連接的數(shù)據(jù)庫名,默認值postgres。如果單指定-U,沒指定-d參數(shù),則默認訪問與用戶名名稱相同的數(shù)據(jù)庫。
- -h hostname 主機名,默認值localhost
- -p port 端口號,默認值5432
4.1 認證方式
常見的四種身份驗證方式
- trust:凡是能連接到服務器的,都是可信任的。只需要提供數(shù)據(jù)庫用戶名,可以沒有對應的操作系統(tǒng)同名用戶;
- password 和 md5:對于外部訪問,需要提供 psql 用戶名和密碼。對于本地連接,提供 psql 用戶名密碼之外,還需要有操作系統(tǒng)訪問權(用操作系統(tǒng)同名用戶驗證)。password 和 md5 的區(qū)別就是外部訪問時傳輸?shù)拿艽a是否用 md5 加密;
- ident:對于外部訪問,從 ident 服務器獲得客戶端操作系統(tǒng)用戶名,然后把操作系統(tǒng)作為數(shù)據(jù)庫用戶名進行登錄;對于本地連接,實際上使用了peer;
- peer:通過客戶端操作系統(tǒng)內(nèi)核來獲取當前系統(tǒng)登錄的用戶名,并作為psql用戶名進行登錄。
4.2 遠程登錄
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
驗證
# server:重啟生效 systemctl restart postgresql-9.6 # client:命令行遠程登錄 psql -U zhangsan -d testdb -h 10.122.45.97 -p 5432
4.3 本地登錄
PostgreSQL登陸默認是peer,不需要驗證用戶密碼即可進入psql相關數(shù)據(jù)庫,但前提是必須切換用戶登陸。類似于最開始執(zhí)行的su postgres;psql
一樣。
[root@sltkp3cbpch data]# psql -U zhangsan -d testdb -p 5432 psql: FATAL: Peer authentication failed for user "zhangsan"
如果必須按照上述登陸方式登陸的話,有兩種修改方式:
- 增添map映射
- 修改認證方式
a. map映射
map
映射是用來將系統(tǒng)用戶映射到對應的postgres數(shù)據(jù)庫用戶,用來限制指定的用戶使用指定的賬號來登陸。
pg_ident.conf
修改pg_ident.conf文件,與pg_hba.conf文件同級目錄。其基本格式如下:
# MAPNAME SYSTEM-USERNAME PG-USERNAME map_zhangsan root zhangsan
- MAPNAME指的是映射的名稱,比如
map_zhangsan
- SYSTEM-USERNAME就是系統(tǒng)用戶的名稱,比如
root
- PG-USERNAME就是數(shù)據(jù)庫里存在的用戶名稱,比如
zhangsan
上面定義的map
意思是:定義了一個叫做map_zhangsan
的映射,當客戶端用戶是root
的時候,允許它用zhangsan
用戶來登陸PostgreSQL。
修改pg_hba.conf文件
在peer的認證方式后面添加:map=map_tom
重啟PostgreSQL服務,再次嘗試,連接成功。
b. 修改認證方式
需要修改一下pg_hba.cong
文件,將local all all peer
修改為local all all md5
,如下圖所示:
重啟PostgreSQL服務,再次嘗試,連接成功。
到此這篇關于postgresql安裝及配置超詳細教程的文章就介紹到這了,更多相關postgresql安裝及配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
postgresql 實現(xiàn)多表關聯(lián)刪除
這篇文章主要介紹了postgresql 實現(xiàn)多表關聯(lián)刪除操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01解決postgreSql遠程連接數(shù)據(jù)庫超時的問題
這篇文章主要介紹了解決postgreSql遠程連接數(shù)據(jù)庫超時的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12postgreSQL如何設置數(shù)據(jù)庫執(zhí)行超時時間
本文我們將深入探討PostgreSQL數(shù)據(jù)庫中的一個關鍵設置SET?statement_timeout,這個設置對于管理數(shù)據(jù)庫性能和優(yōu)化查詢執(zhí)行時間非常重要,讓我們一起來了解它的工作原理以及如何有效地使用它2024-01-01PostgreSQL 實現(xiàn)將多行合并轉(zhuǎn)為列
這篇文章主要介紹了PostgreSQL 實現(xiàn)將多行合并轉(zhuǎn)為列的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12