PostgreSQL_11.1_安裝和基礎配置

環境

CentOS 7 x64linux

PostgreSQL 11.1git

OS參數配置

內核參數的配置,這裏有一篇文章寫的很詳細,建議閱讀。github

sysctl參數

#### 修改系統參數
vi /etc/sysctl.conf

# 修改下列配置(參數根據本身狀況修改)
kernel.shmmax = 500000000
kernel.shmmni = 4096
kernel.shmall = 4000000000
kernel.sem = 500 1024000 200 4096

kernel.sysrq = 1
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.msgmni = 2048
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_forward = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.conf.all.arp_filter = 1
net.ipv4.ip_local_port_range = 1025 65535
net.core.netdev_max_backlog = 10000
net.core.rmem_max = 2097152
net.core.wmem_max = 2097152
vm.overcommit_memory = 2

#### 修改完畢後從新載入
sysctl -p
複製代碼

limits參數

#### 修改文件打開數等限制
vi /etc/security/limits.conf

# 添加以下幾行(注意*也須要添加)
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072

#### 
vi /etc/security/limits.d/90-nproc.conf

# 添加以下幾行(注意*也須要添加)
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
複製代碼

關閉防火牆

#### 關閉防火牆
chkconfig iptables off
service iptables stop
systemctl stop firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#### 檢查修改後的結果
sestatus
複製代碼

安裝數據庫

由於安裝過程涉及到新建用戶等操做,因此這裏使用root用戶(或sudo)來進行準備工做。sql

創建OS用戶

useradd postgres
複製代碼

yum安裝

#### 先安裝RPM庫
yum install -y https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm

#### 安裝服務端和客戶端
yum install -y postgresql11 postgresql11-server
複製代碼

設置目錄權限

mkdir -p /data/pgsql/data
mkdir -p /data/pgsql/log
chown -R postgres.postgres /data/pgsql /usr/pgsql-11/
複製代碼

初始化數據庫

後續的設置使用前面新建的postgres用戶操做數據庫

用戶環境變量

su - postgres

#### 修改環境變量
vi ~/.bash_profile

# 增長以下內容
export PG_HOME=/usr/pgsql-11
export PGDATA=/data/pgsql/data
export PATH=${PG_HOME}/bin:$PATH
export PGPORT=5432
export PGUSER=postgres
export PGDATABASE=postgres

#### 從新載入環境變量
source ~/.bash_profile
複製代碼

初始化並修改參數

#### 使用默認地址${PGDATA}初始化
${PG_HOME}/bin/initdb -D ${PGDATA} -E utf8

#### 設置監聽IP和Port
vi ${PGDATA}/postgresql.conf

# 設置監聽全部IP(這是一個很是寬鬆的限制,生產環境慎用)
listen_addresses = '*'
port = 5432

#### 設置數據庫白名單
vi ${PGDATA}/pg_hba.conf

# 增長以下一條記錄,容許用戶密碼模式(這是一個很是寬鬆的限制,生產環境慎用)
host	all		all		0.0.0.0/0	md5
複製代碼

啓動中止命令

這裏的啓停命令都是用postgres用戶執行的。centos

PS:在生產系統上,咱們建議使用系統服務的方式啓動,詳見平常運維部分。bash

#### 啓動
${PG_HOME}/bin/pg_ctl start

#### 中止
${PG_HOME}/bin/pg_ctl stop

#### 從新載入配置文件(不須要重啓)
${PG_HOME}/bin/pg_ctl reload
複製代碼

訪問數據庫

命令行鏈接

# 使用postgres用戶做爲超級用戶登陸
psql -U postgres -d postgres
複製代碼
-- 創建管理員帳號進行日程操做,避免使用內置超級用戶
create user admin superuser password 'xxx';
複製代碼

新建DB和USER

-- 新建一個測試用用戶
create user demo with password 'demo';

-- 新建一個測試用DB並分配給指定用戶
create database demo with encoding='utf8' owner=demo;

-- 修改用戶密碼
alter user demo password 'xxx';
複製代碼

建立只讀用戶

在GP下建立一個只讀用戶的流程是cookie

  1. 創建用戶
  2. 賦予給定DB的鏈接權限
  3. 賦予給定Schema的使用權限
  4. 賦予給定Table的訪問權限

備註:由於咱們想要精確控制,因此這裏使用的是針對每個表的顯式受權。若是要求不那麼嚴格,可使用默認受權讓新建的表自動得到訪問權限。運維

-- 新建一個測試用用戶(gpadmin用戶執行)
drop user if exists demoread;
create user demoread password 'demo';

-- 賦予、取消給定DB的鏈接權限(DB全部者執行,這裏是demo用戶執行)
grant connect on database demo to demoread;
revoke connect on database demo from demoread;

-- Schema和Table的權限操做用下面語句生成,而後執行
-- (DB全部者執行,這裏是demo用戶執行)
-- 構造對指定schema下全部表的grant、revoke語句
select 
	'grant usage on schema ' || t.table_schema || ' to ' || u.uname || ' ;' as schema_grant,
	'revoke usage on schema ' || t.table_schema || ' from ' || u.uname || ' ;' as schema_revoke,
	'grant select on ' || t.table_schema || '.' || t.table_name || ' to ' || u.uname || ' ;' as table_grant,
	'revoke select on ' || t.table_schema || '.' || t.table_name || ' from ' || u.uname || ' ;' as table_revoke
from information_schema.tables t, 
	(select 'demoread' as uname) as u -- 提供:給定被受權用戶名
where t.table_type in ('BASE TABLE', 'VIEW', 'FOREIGN')
	and t.table_schema in ('s01','s02') -- 提供:受權schema
order by t.table_schema, t.table_name
;
複製代碼

平常運維

啓動服務

由於在初始化數據庫的時候咱們使用了自定義數據目錄,因此在註冊服務前咱們須要修改默認的地址tcp

### 修改服務中的地址
vi  /usr/lib/systemd/system/postgresql-11.service

# 修改Environment=PGDATA=/var/lib/pgsql/11/data/爲
Environment=PGDATA=/data/pgsql/data/

### 註冊服務並啓動
systemctl daemon-reload
systemctl enable postgresql-11
systemctl start postgresql-11
複製代碼

徹底卸載

###
systemctl stop postgresql-11
systemctl disable postgresql-11

###
yum remove -y postgresql11 postgresql11-server

###
rm -rf /usr/pgsql-11/ /var/lib/pgsql/ /data/pgsql/

###
userdel -r postgres
複製代碼

備份恢復

修改數據目錄

若是在安裝中使用的是默認目錄,而後須要修改數據目錄,執行下列操做

### 關閉數據庫
systemctl stop postgresql-11

### 移動目錄
mv /var/lib/pgsql/11/* /data/pgsql/

### 修改服務中的地址
vi  /usr/lib/systemd/system/postgresql-11.service

### 修改.bash_profile
export PGDATA=/data/pgsql/data

### 註冊服務並啓動
systemctl daemon-reload
systemctl start postgresql-11
複製代碼
相關文章
相關標籤/搜索