操作目的:
PostgreSQL數據庫在不同模式之間遷移數據,可用於在異機數據遷移的場景。
今天網友問到一個問題,是在數據遷移的場景中,想把源庫的數據遷移到不同的schema下面,比如從schema gaoqiang,遷移到schema mayday。
schema(模式)這種概念在Oracle中,可以把用戶認爲就是schema,比如用戶gaoqiang的模式就是gaoqiang;在其他數據庫中不一定是一一嚴格對應的,具有一定的靈活性。在PostgreSQL數據庫中,模式和用戶可以單獨創建,也可一起創建。
操作思路:
從備份導出原有的schema gaoqiang的數據--->新建用戶、模式 mayday--->修改相關配置--->導入數據到新的模式Mayday--->驗證數據完整性以及屬性
導出數據庫music中的模式gaoqiang的表結構和數據:
-bash-4.1$ pg_dump -d music -n gaoqiang -f /tmp/gaoqiang.sql
點擊(此處)摺疊或打開
- -bash-4.1$ cat gaoqiang.sql
- --
- -- PostgreSQL database dump
- --
- SET statement_timeout = 0;
- SET lock_timeout = 0;
- SET client_encoding = 'UTF8';
- SET standard_conforming_strings = on;
- SET check_function_bodies = false;
- SET client_min_messages = warning;
- --
- -- Name: gaoqiang; Type: SCHEMA; Schema: -; Owner: gaoqiang
- --
- CREATE SCHEMA gaoqiang;
- ALTER SCHEMA gaoqiang OWNER TO gaoqiang;
- SET search_path = gaoqiang, pg_catalog; ----標紅的2行一個是決定導入到那個schema中,一個是決定表的屬性,還可以設定表空間和oid,如果有需要可以設置
- SET default_tablespace = '';
- SET default_with_oids = false;
- --
- -- Name: summary; Type: TABLE; Schema: gaoqiang; Owner: gaoqiang; Tablespace:
- --
- CREATE TABLE summary (
- id integer,
- name text
- );
- ALTER TABLE summary OWNER TO gaoqiang;
- --
- -- Data for Name: summary; Type: TABLE DATA; Schema: gaoqiang; Owner: gaoqiang
- --
- COPY summary (id, name) FROM stdin;
- 1 GaoQiang
- 2 GaoQiang is not 2
- \.
- --
- -- PostgreSQL database dump complete
- --
創建新的用戶和模式:
music=# \c music postgres
You are now connected to database "music" as user "postgres".
music=# create user mayday with password 'mayday';
CREATE ROLE
music=# create schema authorization mayday;
CREATE SCHEMA
修改pg_dump導出的文件gaoqiang.sql:
把原來的:
SET search_path = gaoqiang, pg_catalog;
改成:
SET search_path = mayday, pg_catalog;
-bash-4.1$ vi gaoqiang.sql
-bash-4.1$ cat gaoqiang.sql |grep mayday
SET search_path = mayday, pg_catalog;
開始遷移:
-bash-4.1$ psql music
SET
SET
SET
SET
SET
SET
ERROR: schema "gaoqiang" already exists
ALTER SCHEMA
SET
SET
SET
CREATE TABLE
ALTER TABLE
COPY 2
-bash-4.1$
用mayday登錄數據庫查看錶的屬性:
-bash-4.1$ psql music mayday
psql (9.4.1)
Type "help" for help.
查看錶屬性:
music=> \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
mayday | summary | table | gaoqiang ---發現該表的屬主屬性有點問題
(1 row)
處理方法有2種,都很簡單:
方法1:
-bash-4.1$ psql music postgres
psql (9.4.1)
Type "help" for help.
music=# alter table mayday.summary OWNER TO mayday;
ALTER TABLE
music=# \c music mayday
You are now connected to database "music" as user "mayday".
music=> \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+--------
mayday | summary | table | mayday
(1 row)
方法2:
先刪除剛纔的測試表,然後再進行導入操作,避免衝突:
music=> drop table summary;
DROP TABLE
在導出的腳本中有這麼一行:
ALTER TABLE summary OWNER TO gaoqiang;
在修改模式路徑的時候,直接修改該語句也可。
-bash-4.1$ vi /tmp/gaoqiang.sql
ALTER TABLE summary OWNER TO mayday;
music=> \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | summary | table | postgres
(1 row)
music=> \q
開始遷移:
-bash-4.1$ psql music
SET
SET
SET
SET
SET
SET
ERROR: schema "gaoqiang" already exists
ALTER SCHEMA
SET
SET
SET
CREATE TABLE
ALTER TABLE
COPY 2
-bash-4.1$
驗證表的屬主和模式已改變:
-bash-4.1$ psql music mayday
psql (9.4.1)
Type "help" for help.
music=> \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+--------
mayday | summary | table | mayday
(1 row)
驗證數據完整性與屬性:
用DBA用戶連接數據庫查詢2張不同模式的表:
music=> \c music postgres
You are now connected to database "music" as user "postgres".
music=# select * from gaoqiang.summary;
id | name
----+-------------------
1 | GaoQiang
2 | GaoQiang is not 2
(2 rows)
music=# select * from mayday.summary;
id | name
----+-------------------
1 | GaoQiang
2 | GaoQiang is not 2
(2 rows)
music=# \c music gaoqiang
You are now connected to database "music" as user "gaoqiang".
music=> \d
List of relations
Schema | Name | Type | Owner
----------+---------+-------+----------
gaoqiang | summary | table | gaoqiang
(1 row)
music=# select tablename,tableowner,schemaname from pg_tables where tablename = 'summary';
tablename | tableowner | schemaname
-----------+------------+------------
summary | gaoqiang | gaoqiang
summary | mayday | mayday
(4 rows)