centos7.5安裝postgresql10

記錄一下centos7.5下安裝postgresql10的過程sql

安裝

1 更新源數據庫

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm -y

2 安裝postgresqlvim

yum install postgresql10-contrib postgresql10-server -y

3 初始化數據庫centos

/usr/pgsql-10/bin/postgresql-10-setup initdb

4 啓動服務bash

systemctl start postgresql-10

5 設置密碼
postgresql 會默認添加一個用戶postgres,咱們須要給他設置一下密碼post

su - postgres
psql
ALTER USER postgres WITH PASSWORD 'yourpassword';

到這裏已經能夠使用了,可是爲了更方便的使用,咱們還能夠進行其餘設置。centos7

遠程登陸

postgresql默認不支持遠程登陸,咱們須要修改配置文件pg_hba.confrest

vim /var/lib/pgsql/10/data/pg_hba.conf

修改前
postgresql

修改後
code

而後修改另外一個配置文件postgresql.conf

vim /var/lib/pgsql/10/data/postgresql.conf

修改前

修改後

重啓postgresql

systemctl restart postgresql-10

以後就能夠用pgadmin遠程登陸了。

開機自啓動

設置開機啓動

systemctl enable postgresql-10.service

備份與恢復

postgresql的備份命令爲

pg_dump -h ipaddress -p port -U user dbname>dumpfile

例如,將本地數據庫postgres備份到/var/www/backup.sql文件中

pg_dump -h 127.0.0.1 -p 5432 -U postgres postgres>/var/www/backup.sql

postgresql的恢復命令爲

psql -h ipaddress -p port -U user dbname<dumpfile

例如,從/var/www/backup.sql恢復數據庫到本地postgres中

psql -h 127.0.0.1 -p 5432 -U postgres postgres</var/www/backup.sql

值得一提的是,恢復命令不會建立數據庫,因此須要自行建立一個新的數據庫(在上例中,新數據庫名還是postgres)

定時備份

由備份命令咱們能夠建立一個腳原本自動化備份數據。

我在/var/www/下新建一個文件dailybackup.sh,腳本內容大體以下

#!/bin/bash

cur_day=$(date '+%Y%m%d')

echo "start backup postgresql..."

# 執行備份
pg_dump -h 127.0.0.1 -p 5432 -U postgres postgres>/var/www/backup_$cur_day.sql

echo "finish backup."

記得順便給該文件授予可執行權限

再新建一個定時任務,執行命令

crontab -e

輸入以下內容,意思是天天凌晨1點執行/var/www/dailybackup.sh

0 1 * * * /var/www/dailybackup.sh

保存退出,由此自動化備份數據庫完成。

相關文章
相關標籤/搜索