close

這篇文章的起因是因工作需要,

從一台舊的主機上把postgresql資料dump出來並且移到另一台新的主機,

 

環境

舊主機上的PostgreSQL 版本9.1.14

新主機上的PostgreSQL 版本9.6

 

第一部 - 安裝client

安裝postgresql client 9.1版,以便備份舊系統的DB (此處沒有常是新版有無向下相容,一般來說會有)

修改repository設定檔,新增兩行

vi /etc/yum.repos.d/CentOS-Base.repo

[base]

...

exclude=postgresql*

[updates]

...

exclude=postgresql*

下載PGDG RPM並執行

wget https://download.postgresql.org/pub/repos/yum/9.1/redhat/rhel-6-x86_64/pgdg-centos91-9.1-6.noarch.rpm

rpm -ivh pgdg-centos91-9.1-6.noarch.rpm

查看有無postgresql packages

yum list postgresql*

其中會列出,要的9.1版

postgresql91.x86_64                                                            9.1.24-2PGDG.rhel6                                               @pgdg91

安裝postgresql client (需有裝EPEL)

yum install postgresql91.x86_64 -y

備份全部資料庫

pg_dumpall -h host -U user -W -f backup.sql

或者

備份特定資料庫

pg_dump dbname -h host -U user -W -f backup.sql

 

 

第二部 - 安裝Server

照樣修改/etc/yum.repos.d/CentOS-Base.repo

下載PGDG RPM並執行

wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm

rpm -ivh pgdg-centos96-9.6-3.noarch.rpm

查看有無postgresql packages

yum list postgresql*

其中會列出,需要的9.6版

postgresql96-server.x86_64                                                      9.6.1-1PGDG.rhel6                                                 pgdg96

安裝(client會自動安裝)

yum install postgresql96-server.x86_64 -y

初始化資料庫

service postgresql-9.6 initdb

啟動服務 + 開機啟用

service postgresql-9.6 start

chkconfig postgresql-9.6 on

 

登入Postgresql

切換成postgresql預設的管理者(postgres預設是linux帳號當作postgresql帳號)

su - postgres

登入(預設會登入資料庫postgres, psql -l 可以列出所有資料庫)

psql

建立使用者

postgres=#CREATE USER user WITH ENCRYPTED PASSWORD 'xxxx';

超級管理者權限

postgres=#ALTER ROLE user WITH superuser;

postgres=# \du
                              角色清單
 角色名稱 |                     屬性                                          | 成員屬於
----------+-----------------------------------------------+----------
 [user]      | 超級用戶                                                       | {}
 postgres | 超級用戶, 建立角色, 建立 DB, 複製, Bypass RLS | {}

登出

postgres=#\q

 

修改postgresql設定,啟用遠端tcp/ip連線,(僅修改host部分)

vi /var/lib/pgsql/9.6/data/pg_hba.conf

# 可以透過 tcp/ip 從 127.0.0.1/32 及 192.168.0.0/24 登入
host    all         all         127.0.0.1/32          md5
host    all         all         192.168.0.0/24        md5

vi /var/lib/pgsql/9.6/data/postgresql.conf
listen_addresses='localhost'   =>      listen_addresses='*'

重啟服務,並開啟防火牆port 5432
service postgresql-9.6 restart

檢查port
netstat -an | grep 5432

 

接著到另一台事先裝好postgresql 9.6 client的主機登入,(此處的dbname可以使用預設的DB postgres)

psql -h host -d dbname -U user -W

建立好資料庫(create 之前不能有空格,dbname的跳脫自原為 " 雙引號)

Create Database dbname;

還原資料庫

psql -h host -f backup.sql dbname user

大功告成

 

 

參考

PSQL YUM Installation

PostgreSQL的備份與復原

在 RHEL/CENTOS 6 Debian Linux 上使用 PostgreSQL資料庫

arrow
arrow
    文章標籤
    centos Postgresql
    全站熱搜

    abcg5 發表在 痞客邦 留言(0) 人氣()