PostgreSQL不同模式(SCHEMA)之間遷移數據

操作目的:
  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

 

點擊(此處)摺疊或打開

  1. -bash-4.1$ cat gaoqiang.sql
  2. --
  3. -- PostgreSQL database dump
  4. --
  5.  
  6. SET statement_timeout = 0;
  7. SET lock_timeout = 0;
  8. SET client_encoding = 'UTF8';
  9. SET standard_conforming_strings = on;
  10. SET check_function_bodies = false;
  11. SET client_min_messages = warning;
  12.  
  13. --
  14. -- Name: gaoqiang; Type: SCHEMA; Schema: -; Owner: gaoqiang
  15. --
  16.  
  17. CREATE SCHEMA gaoqiang;
  18.  
  19.  
  20. ALTER SCHEMA gaoqiang OWNER TO gaoqiang;
  21.  
  22. SET search_path = gaoqiang, pg_catalog; ----標紅的2行一個是決定導入到那個schema中,一個是決定表的屬性,還可以設定表空間和oid,如果有需要可以設置
  23.  
  24. SET default_tablespace = '';
  25.  
  26. SET default_with_oids = false;
  27.  
  28. --
  29. -- Name: summary; Type: TABLE; Schema: gaoqiang; Owner: gaoqiang; Tablespace:
  30. --
  31.  
  32. CREATE TABLE summary (
  33.     id integer,
  34.     name text
  35. );
  36.  
  37.  
  38. ALTER TABLE summary OWNER TO gaoqiang;
  39.  
  40. --
  41. -- Data for Name: summary; Type: TABLE DATA; Schema: gaoqiang; Owner: gaoqiang
  42. --
  43.  
  44. COPY summary (id, name) FROM stdin;
  45. 1    GaoQiang
  46. 2    GaoQiang is not 2
  47. \.
  48.  
  49.  
  50. --
  51. -- PostgreSQL database dump complete
  52. --





創建新的用戶和模式:
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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章