CentOS/RHEL 7上PostgreSQL的安裝配置與基本使用
安裝
Install the repository RPM
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm
Install the client packages
yum install postgresql96
Install the server packages
yum install postgresql96-server
Initialize the database and enable automatic start
/usr/pgsql-9.6/bin/postgresql96-setup initdb
systemctl enable postgresql-9.6
systemctl start postgresql-9.6?
配置
編輯/var/lib/pgsql/9.6/data/postgresql.conf,修改listen_addresses,監聽所有地址:
listen_addresses = ‘*‘
編輯/var/lib/pgsql/9.6/data/pg_hba.conf,修改認證方式:
# "local" is for Unix domain socket connections only local?? all???????????? all???????????????????????????????????? trust # IPv4 local connections: host??? all???????????? all???????????? 127.0.0.1/32??????????? ident host??? all???????????? all???????????? 0.0.0.0/0????????????????? md5
重啟PostgreSQL
systemctl restart postgresql-9.6?
認證方式
認證方式支持"trust", "reject", "md5", "password", "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" , "cert"。
- trust? 任何人都可以訪問數據庫,需要指定數據庫用戶名。如上,本地可以使用psql -U postgres連接數據庫(當未指定數據庫用戶名時,默認為root)。
- password? 密碼認證,發送明文密碼
- md5? 密碼認證,發送經MD5加密的密碼,假如數據庫服務器IP是10.188.13.29,則可以這樣訪問:psql -h 10.188.13.29 -U postgres,回車後會提示輸入密碼。
- ident? 從ident server獲取客戶端操作系統的用戶名,當與數據庫用戶名匹配時則可訪問。當ident配置在local連接時,將使用peer替代。存在安全隱患,僅適用於封閉網絡,不建議使用。
- peer? 從kernel獲取客戶端操作系統的用戶名,當與數據庫用戶名匹配時則可訪問,僅用於local連接。如local配置為peer時,可以這樣訪問psql -U postgres
當操作系統用戶名與數據庫用戶名不一致時可以在文件pg_ident.conf中配置map關系,如下:# MAPNAME?????? SYSTEM-USERNAME???????? PG-USERNAME omicron????????????? root?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? postgres
然後在pg_hba.conf中配置使用map:
local?? all???????????? all??????????????????????????????????? ? ? ? ?? peer map=omicron host??? all???????????? all???????????? 127.0.0.1/32??????????? ident map=omicron
PSQL
連接PostgreSQL
psql -U postgres
更多參數可以查看幫助psql --help
?
刷新配置
修改配置文件後,可執行以下命令刷新配置:
select pg_reload_conf();
?
更改密碼
ALTER USER postgres WITH PASSWORD ‘postgres‘;
?
查看用戶
select * from pg_shadow;
?
查看data文件夾所在目錄
show data_directory;
?
創建用戶
CREATE USER test WITH PASSWORD ‘test‘;
ALTER USER test WITH SUPERUSER;
?
創建SCHEMA
CREATE SCHEMA test;
ALTER SCHEMA test OWNER TO test;
?
查看SCHEMA
\dn
?
設置Search Path
SET search_path TO test;
?
執行sql腳本
\i test.sql
?
Sequence
查詢sequence(currval(), nextval())
select nextval(‘test_sequence‘);
更新sequence
alter sequence test_sequence restart with 42;
?
退出
\q
?
幫助
help
\?
\h
?
備份與恢復
pg_dump -h host1?-U postgres [-n schema]?dbname > outfile
psql -U postgres dbname < infile
?
也可直接備份data目錄
tar -cf backup.tar /usr/local/pgsql/data
存儲過程
清空所有表數據的一個小存儲過程(schema名稱為test):
--?FUNCTION:?test.truncatealltable()??
??
--?DROP?FUNCTION?test.truncatealltable();??
??
CREATE?OR?REPLACE?FUNCTION?test.truncatealltable()??
????RETURNS?text??
????LANGUAGE?‘plpgsql‘??
??
AS?$BODY$??
??
DECLARE??
????cur_all_tables?CURSOR?FOR??
??????select?relname?from?pg_class??
??????where?relnamespace?=?(select?oid?from?pg_namespace?where?nspname?=?‘test‘)??
????????and?relkind?=?‘r‘?order?by?relname;??
????truncate_sql?CHARACTER?VARYING(100);??
?????
BEGIN??????
????FOR?record?IN?cur_all_tables??
????LOOP?????????????
????????truncate_sql?:=?concat(‘truncate?table?test.‘,?record.relname,?‘?cascade‘);??
????????EXECUTE?truncate_sql;??????????
????END?LOOP;??
??
????return?‘success‘;??
END??
??
$BODY$;??
PostgreSQL
CentOS/RHEL 7上PostgreSQL的安裝配置與基本使用