一、安裝
1.1 根據(jù)操作系統(tǒng)選擇yum倉庫
https://www.postgresql.org/download/linux/redhat/
根據(jù)操作系統(tǒng),選擇yum倉庫:
這里我們選擇的是Centos7:
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
1.2 yum安裝
第一步:
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
第二步:
安裝
yum install postgresql12-server
成功如圖:
第三步:
安裝第三方依賴庫:
yum install postgresql12-libs
yum install postgresql12-contrib
yum install postgresql12-devel
1.3 初始化數(shù)據(jù)庫
/usr/pgsql-12/bin/postgresql-12-setup initdb
1.4 設(shè)置為開機(jī)啟動
systemctl enable postgresql-12
1.5 啟動postgresql
systemctl start postgresql-12
1.6 設(shè)置linux用戶postgres的密碼
默認(rèn)情況下,一旦postgresql安裝完畢之后颤诀,linux會自動為postgresql數(shù)據(jù)庫生成一個名為postgres的用戶,我們可以修改該用戶的密碼。
這里我們把密碼設(shè)置為:postgres
[root@iZ8vba1umdjvda1s95cyf6Z ~]# passwd postgres
Changing password for user postgres.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@iZ8vba1umdjvda1s95cyf6Z ~]#
1.7 修改數(shù)據(jù)庫用戶postgres的密碼
在postgresql安裝完成后温亲,默認(rèn)有一個數(shù)據(jù)庫用戶postgres,我們可以為其設(shè)置密碼:首先杯矩,我們先在linux系統(tǒng)上栈虚,由root用戶切換到postgres用戶下:
[root@iZ8vba1umdjvda1s95cyf6Z ~]# su - postgres
然后執(zhí)行如下命令:
psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'NewPassword';"
1.8 登錄數(shù)據(jù)庫并查看數(shù)據(jù)庫版本
以postgres用戶的身份登錄postgresql數(shù)據(jù)庫:
登錄命令: psql -U postgres
-bash-4.2$ psql -U postgres
psql (9.2.24, server 12.3)
WARNING: psql version 9.2, server version 12.0.
Some psql features might not work.
Type "help" for help.
postgres=#
查詢數(shù)據(jù)庫的版本信息:
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)
postgres=#
1.9 配置監(jiān)聽參數(shù)
修改配置文件:/var/lib/pgsql/12/data/postgresql.conf
[root@iZ8vba1umdjvda1s95cyf6Z ~]# vim /var/lib/pgsql/12/data/postgresql.conf
修改為:監(jiān)聽所有的ip,端口號為:5432
重啟pg:
systemctl restart postgresql-12
查看運行狀態(tài):
[root@iZ8vba1umdjvda1s95cyf6Z ~]# systemctl status postgresql-12
顯示如下:
[root@iZ8vba1umdjvda1s95cyf6Z ~]# systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-06-16 14:55:49 CST; 25min ago
Docs: https://www.postgresql.org/docs/12/static/
Process: 28528 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 28535 (postmaster)
CGroup: /system.slice/postgresql-12.service
├─28535 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
├─28537 postgres: logger
├─28540 postgres: checkpointer
├─28541 postgres: background writer
├─28542 postgres: walwriter
├─28543 postgres: autovacuum launcher
├─28544 postgres: stats collector
├─28545 postgres: logical replication launcher
└─31015 postgres: postgres postgres [local] idle
2.0 配置遠(yuǎn)程訪問
這個pg_hba.conf文件是控制客戶端認(rèn)證的配置文件史隆。(HBA stands for host-based authentication)
(詳情鏈接:https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html)
編輯配置文件:
[root@iZ8vba1umdjvda1s95cyf6Z ~]# vim /var/lib/pgsql/12/data/pg_hba.conf
修改為:
重啟數(shù)據(jù)庫:
[root@iZ8vba1umdjvda1s95cyf6Z ~]# systemctl restart postgresql-12
2.1 防火墻放開5432端口
[root@iZ8vba1umdjvda1s95cyf6Z ~]# firewall-cmd --add-port=5432/tcp --permanent
success
[root@iZ8vba1umdjvda1s95cyf6Z ~]# firewall-cmd --reload
success
[root@iZ8vba1umdjvda1s95cyf6Z ~]#
測試端口連通性:
[root@iZ8vba1umdjvda1s95cyf6Z ~]# nc -w 1 87.99.230.18 5432 < /dev/null && echo "tcp port ok"
tcp port ok
[root@iZ8vba1umdjvda1s95cyf6Z ~]#
到此魂务,我們就可以用Navicat連接pg數(shù)據(jù)庫了。
2.2 創(chuàng)建數(shù)據(jù)庫和用戶
2.2.1 創(chuàng)建一個用戶并指定密碼:
切換到postgres用戶:
[root@iZ8vba1umdjvda1s95cyf6Z ~]# su postgres
然后登陸pg:
psql -U postgres
創(chuàng)建用戶并指定密碼:
postgres=# create user myuserwith password 'PASSWORD';
CREATE ROLE
postgres=#
2.2.2 創(chuàng)建數(shù)據(jù)庫并指定屬主
postgres=# create database mydb owner myuser;
2.2.3 數(shù)據(jù)庫賦權(quán)
數(shù)據(jù)庫賦權(quán)泌射,未賦權(quán)則賬戶只能登錄控制臺粘姜。
postgres=# grant all privileges on database mydb to myuser;
GRANT
postgres=#
2.3 卸載postgresql
yum remove postgresql*
二、其他配置
2.1 時區(qū)配置
查詢時區(qū):
SHOW timezone;
設(shè)置服務(wù)器的時區(qū)
vim /var/lib/pgsql/12/data/postgresql.conf
重啟:
[root@iZ8vba1umdjvda1s95cyf6Z ~]# systemctl restart postgresql-12
2.2 服務(wù)器編碼集設(shè)置
設(shè)置默認(rèn)字符集
[root@iZ8vba1umdjvda1s95cyf6Z ~]# su postgres
bash-4.2$ psql -U postgres
could not change directory to "/root"
psql (9.2.24, server 12.3)
WARNING: psql version 9.2, server version 12.0.
Some psql features might not work.
Type "help" for help.
postgres=# initdb -E UTF8;
創(chuàng)建數(shù)據(jù)庫時指定編碼:
postgres-# create database hercules with encoding 'UTF-8' template template0 lc_collate='zh_CN.utf8' lc_ctype='zh_CN.utf8'
postgres-#
三熔酷、備份及恢復(fù)
3.1 備份
備份命令:pg_dump
pg_dump可用于將一個PostgreSQL的數(shù)據(jù)提取成一個腳本文件或歸檔文件孤紧。
pg_dump only dumps a single database. To back up an entire cluster, or to back up global objects that are common to all databases in a cluster (such as roles and tablespaces), use pg_dumpall.
pg_dump dbname > dumpfile
示例:
pg_dump mydb > db.sql
3.2 恢復(fù)
pg_restore命令
更多信息,請訪問postgresql官網(wǎng):https://www.postgresql.org