Calendar for PostgreSQL | Advent Calendar 2021 - Qiita
ã®10æ¥ç®ã®è¨äºã§ãã
æãå½ããç¯ããã人ã¯ãä»ã®è¦ç©ããã®æ°åã®æéã¨äºç®ã確ä¿ãã¦ããããã¯ãªããã¦æ¬²ããã
å¤åããã®ã¾ã¾èµ°ãã¨èª°ãåããããããã
ã§çµããã«ããããã«ã¯ãããªãã®ã§ãããã¤ãæ¸ãã¾ãã
ãã¡ãããSQLãDBMSã«ãã£ã¦ãæ¹è¨ããããã®ã§ã使ããæ§æãé¢æ°åãéãã£ã¦ã®ã¯å½ç¶ããã®ã§ããããã以å¤ã®ã¨ãããä¸å¿ã«ã
PostgreSQLã®ãµã¼ãã¼ãµã¤ãã¨ã³ã³ã¼ãã£ã³ã°ã¯ShiftJISããµãã¼ããã¦ããªã
ä»ã©ããæ°è¦ã§ç«ã¦ããã¼ã¿ãã¼ã¹ã¯UTF8ãé¸ã¶ããã¼ãã£ãããã¨æã£ã¦ããä½ä¸ä»£ããã¼ã¸ã§ã³ã¢ãããã¦ããOracleãã塩漬ããããOracleãªããã¯ãShiftJISã使ã£ã¦ããã¨ãããå¤ãã®ã§ã¯ãªãã§ããããã
èªåã§æåã³ã¼ããããã³ã°ã®å¦çãæ¸ããªãã¦ããETLãã¼ã«çã§ShiftJISãªOracleããUTF8ãªPostgreSQLã«ä¸æããã¨è»¢éã§ãããã¯ãã¾ãããæ¬å½ã«æ£ãããã¼ã¿ãæ ¼ç´ããã¦ãããã®æ¤è¨¼ã«ã¯ååãªæéã¨ä½å¶ãåã£ã¦ãã ããããã¨ã
ã¡ãªã¿ã«ãPostgreSQLãã¯ã©ã¤ã¢ã³ãå´ã¯ShiftJISã«ã¯å¯¾å¿ãã¦ãããããã¡ããã³ã³ã§ããæå¾ ããã¨ããã«å¤æããã¦ããã¯ç¢ºèªãå¿ è¦ã§ãã
ã¾ããWindowsã®ã³ãã³ãããã³ããããpsqlã§UTF8ãªPostgreSQLã«å¯¾ãã¦SQLãå©ããã¨ããã¨ããä½è¨ãªã²ã¨æéå
¥ãã®ã§è¦æ³¨æã
PostgreSQLã«ããããªãã¸ã§ã¯ãåã®è©ä¾¡ã¯ãååãå°æåã
Oracleæååã¯ããªãã¸ã§ã¯ãåã¯å¤§æåã§æ¸ããã¨ãå¤ãã§ããããPostgreSQLã«ããã¦ãªãã¸ã§ã¯ãåã®ç®¡çã®ããã©ã«ãã¯ãå°æåã§ä¿åããå°æåã§è©ä¾¡ãããã§ãã
PostgreSQLã§DDLã大æåã§çºè¡ãã¦ãå°æåã§ä¿åããã¾ãããDMLã大æåãã¼ã¹ã§æãã¦ããå°æåã¨ãã¦ãªãã¸ã§ã¯ãç §åããã¾ãã
PostgreSQLã§ã¯ã大æåã§å®ç¾©ãããªãã¸ã§ã¯ãã¨å°æåã§å®ç¾©ãããªãã¸ã§ã¯ãã¯å¥ã¢ãã¨ã¿ãªããã¾ãã
大æåã§å®ç¾©ãããå ´åãã大æåã§å®ç¾©ãããªãã¸ã§ã¯ããåç §ããã«ã¯ãããã«ã¯ãªã¼ãã§å²ãå¿ è¦ãããã¾ãã
testdb=# create table t1 ( testdb(# col1 bigint, testdb(# col2 character varying (1000) testdb(# ) ; CREATE TABLE testdb=# testdb=# testdb=# create table T1 ( testdb(# col1 bigint, testdb(# col2 character varying (1000) testdb(# ) ; ERROR: relation "t1" already exists testdb=# testdb=# create table "T2" ( testdb(# col1 bigint, testdb(# col2 character varying (1000) testdb(# ) ; CREATE TABLE testdb=# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | T2 | table | postgres public | t1 | table | postgres (2 rows) testdb=# \d t2 Did not find any relation named "t2". testdb=# \d T2 Did not find any relation named "T2". testdb=# \d "T2" Table "public.T2" Column | Type | Collation | Nullable | Default --------+-------------------------+-----------+----------+--------- col1 | bigint | | | col2 | character varying(1000) | | |
ãããããã¨ã§ãã
ããã«ã¯ãªã¼ãå²ã¿ãªãã§ãT1ãã§æããCREATE TABLEæã¯ããt1ãã¼ãã«æ¢ã«ããããã£ã¦å¼¾ããã¦ã¾ããã
ããã«ã¯ãªã¼ãå²ã¿ããã§ãT2ãã§æããCREATE TABLEæã¯ã大æåãT2ãã®ãã¼ãã«ã¨ãã¦å®ç¾©ããã¦ã¾ããã
大æåãT2ããã¼ãã«ãç
§ä¼ããã®ã«ãããã«ã¯ãªã¼ãã§å²ã¾ãªãã¨ããt2ãã¼ãã«ããªããï¼ãã£ã¦å¼¾ããã¦ã¾ããã
ãã¼ãã«ã ãã§ã®ãµã³ãã«ã§ãããã«ã©ã ãã¤ã³ããã¯ã¹ãããã·ã¼ã¸ã£çããªãã¸ã§ã¯ãåå ¨é¨ããã®ããã«ç®¡çããã¾ãã
ãªãã¸ã§ã¯ã移è¡ãã¼ã«ãä¸æããã¨å¯¾å¿ãã¦ãªãï¼ã½ã¼ã¹DBã¨ãªãOracleã®å®ç¾©ã常ã«ããã«ã¯ãªã¼ãå²ã¿ã§æ½åºãã¦ãããããªï¼ãã¤ã ã¨ããããã£ã¦ãã¼ã«ã§èªåã§ãã§ãã§ã¤ããã§ã¤ï¼ï¼å¤ï¼ãã ããè¦ç©ããã³ã¹ãç©ãã§ãªãããã£ã¦ã¨ãã«æ²æ¨ãªãã¨ã«ã
ããã«ã¯ãªã¼ãå²ã¿ã§å¼ã£ãæãã¦ããOracleã®ãã¼ãã«å®ç¾©ï¼DDLï¼ããPostgreSQLã§æµããæä½éã®ææ³ä¿®æ£ã ããã¦PostgrSQLã«æµãè¾¼ãã¨ãå ¨é¨ã大æåãã§ä½æããã¾ãã
ã¢ããªã«åãã¦ããSQLããå ¨DBãªãã¸ã§ã¯ãã«ã¤ãã¦ããã«ã¯ãªã¼ãå²ã¿ã§æ¸ãã¦ããã¾ããï¼ã¾ãããããªãã§ããã
ãã£ã¦ãSQLææ³çã«PostgreSQLã«éããã®ã§ããå°æåã§ç»é²ããã¦ãããªãã¸ã§ã¯ããæ¢ãã«ããã®ã§ããããªã¤ãããã¼ããã£ã¦ã¨ã©ã¼ã«ãªãã¾ãã
åè§è±åã ãã§å®ç¾©ããã¦ããã°ãä¸å¾ã§å°æåã«å¼·å¶å¤æããã°å¤§ä¸å¤«ã¨ããã°å¤§ä¸å¤«ãªãã§ããã©ãã
ãã«ããã¤ãæåããªãã¸ã§ã¯ãåã«ä½¿ç¨ãã¦ããDBã®ç§»è¡ãªããã«ã¶ã¤ãã£ãæ¥ã«ã¯ãç®ãå½ã¦ãããªãã§ãã
Oracleã¯ãããã«ã¯ãªã¼ãå²ã¿ãããã«ããã¤ããå©ç¨ãããªãã¸ã§ã¯ãåããæ±ãã¨ãã«å©ç¨ãã¾ããããããã®ç®çã§ã®ã¿ä½¿ã£ã¦ããã±ã¼ã¹ã®æ¹ãå¤ãã§ãããã
ãã£ã¦ããã«ããã¤ããªãªãã¸ã§ã¯ãåã使ã£ã¦ããOracleDBãå©ç¨ããã¢ããªã³ã¼ãå ã«ããã¦ããããã«ã¯ãªã¼ãå²ã¿ã§SQLãè¨è¿°ããã¦ããã¯ãã§ãã
ããã¨ä»åº¦ã¯ãï¼ã¤ã®ãªãã¸ã§ã¯ãåã«ãã«ããã¤ãã¨è±åãæ··å¨ããã¨ãã«ãã«ãªã¹ãçã¾ãã¾ãã
ããã®å ¨è§è±åã¯ãå°æåã§ç§»æ¤ãã¹ããï¼å¤§æåã§ç§»æ¤ãã¹ããï¼ã
â»Oracleã¯ã¨ãããã¼ã¸ã§ã³ããããªãã¸ã§ã¯ãåã«ãã«ããã¤ãæåã使ãã¨ãã¯ããã«ã¯ãªã¼ãå²ã¿ãå¿
é ãã¨è¦å®ããã¾ããã
ãã®ãã¼ã¸ã§ã³ããå¤ãã¨ããã«ã¯ãªã¼ãã§å²ããªãå®è£
ã®ããã®ã·ã¹ãã ãããã
ããã¦ãããï¼ããã«ã¯ãªã¼ãã§å²ã£ã¦ããªãï¼ã§ãè¦å®å¾ã®ãã¼ã¸ã§ã³ã使ã£ã¦ãã¦ãä¸å¿åãã¦ããã·ã¹ãã ããããï¼å½ç¶ãµãã¼ãå¤ï¼
PostgreSQLã«ã¯ã·ããã ããªãã
Oracle大好ããããï¼è¦³æ¸¬ããéãï¼ãããªããã·ããã 大好ãããã
ã§ããPostgreSQLã«ã¯ã·ããã ããªãã®ã§ãã
ã¢ããªå
ã«æ¸ãã¦ããSQLããå
¨é¨ã·ããã åãããªããã¼ãã«æ¬ä½åã§ã¢ã¯ã»ã¹ããããã«æ¸ãæãã¾ããï¼
ããã¦ãã¢ããªå±ããã«æå¦ããã¾ãããã
å ¨åå ¨è¡åç §ãããã¥ã¼ã«ç½®ãæããã°ãä¸å¿ã¯å¯¾å¦ã§ãã¾ãã
é å¼µã£ã¦ãã·ããã å ¨é¨ãèªã¿è§£ãã¦ãã²ãããCREATE VIEWæãæ¸ãã¦ãã ãããCREATE SYNONYMæã£ã¦çãã¦ããã£ããªãã¨ãæããªããã
ã·ããã ã®ã¦ã©ã«ãããã®ãããã¼ãã«ã ã£ããããã¥ã¼ã ã£ããããããªã¢ã©ã¤ãºãã»ãã¥ã¼ã ã£ããããã
ãã¼ã¿ãã¼ã¹ãªã³ã¯ã¯ä½¿ãããã©-ãã®ï¼ï¼ãã¼ã¿ãã¼ã¹ãªã³ã¯ãå®ç¾ããæ©è½ã¯ï¼ã¤ãããããã
Oracle大好ããããï¼è¦³æ¸¬ããéãï¼ãããªãããã¼ã¿ãã¼ã¹ãªã³ã¯ã大好ãããããã ãããå¯æãä½ã£ã¦ï¼å¤ç¨ãããã¦ç®¡çã§ããªããªã£ã¦ï¼æãç¾åãã«ãªã£ã¦ãã人ãå¤ãã§ãã
PostgreSQLã«ããã¼ã¿ãã¼ã¹ãªã³ã¯ç¸å½ã®æ©è½ã¯ããã¾ããä¸è¬çã«ã¯ãdblinkï¼ãã®ã¾ãã¾ï¼ã¨fdwï¼Foregin Data Wrapperï¼ã¨ãããæ¡å¼µæ©è½ã§å®ç¾ããã¦ãã¾ãã
ã
dblinkã¯ãOracleã¨ã¯ã¾ãã§ç°ãªãæ§æã§ãªã¢ã¼ããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ããå¿
è¦ãããããã£ã¦ãã¢ããªã³ã¼ãå
ã®SQLããç¸å½ãªéã®æ¸ãæããçºçããã§ãããã
SELECT * FROM dblink('ã³ãã¯ã·ã§ã³å','SELECT * FROM foo') AS t(a int, b text, c text[]);ãã»ã»ã»dblink()é¢æ°ã®ä¸ã§SELECTæãè¨è¿°ããããã«ãæ»ãã®åå®ç¾©ãè¨è¿°ããå¿ è¦ãããã
ã¨ã
SELECT dblink_exec('insert into foo values(21,''z'',''{"a0","b0","c0"}'');â);ãã»ã»ã»æ´æ°ç³»ã¯dblink_exec()é¢æ°å ã§SQLãæ¸ããã¨ã«ãªããæååã¯âã2ã¤éãããã¨ã«ãªãã
ã¨ããã£ãå ·åã§ãã
fdwã¯ãããå°ãOracleã®ãã¼ã¿ãã¼ã¹ãªã³ã¯ã«è¿ããæ®éã®SQLã¨åãæ¸ãæ¹ã§ãªã¢ã¼ããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ã§ãã¾ãã
ãã ããäºåã«å¤é¨ãã¼ãã«ï¼Foreign tableï¼ã¨ãã¦ãããªã¢ã¼ããã¼ã¿ãã¼ã¹ã«ã©ã®ã¦ã¼ã¶ã¼ã§æ¥ç¶ããã©ã®ã¹ãã¼ãã®ãã©ã®ãã¼ãã«ã«ã¤ãã¦ãã©ããªè¡¨å®ç¾©ã§ãã¢ã¯ã»ã¹ãããã¨ã«ãªãã®ããå®ç¾©ãã¦ããå¿ è¦ãããã¾ãã
ãªã¢ã¼ãå´ã®ãã¼ãã«å®ç¾©ã«å¤æ´ããã£ãããé½åº¦å®ç¾©ããªãããªãã¨ããã®å¤æ´åã¯ãã¼ã«ã«å´ã«ã¯åæ ãããªãã§ãã
ã¾ããå¤é¨ãã¼ãã«åã¯ããã¼ã«ã«å´ã®PostgrSQLã®åãã¹ãã¼ãå ã§ãã¼ãã«ããã¥ã¼ã¨ååè¡çªãèµ·ããããã£ã¦ãæå ã®ãã¼ãã«åã¨åããã¼ãã«åã®ãªã¢ã¼ã表ã«ã¢ã¯ã»ã¹ãããå ´åã¯ãå¥åã§ç®¡çãã¦ããå¿ è¦ãããã¾ãã
å¤é¨ãã¼ãã«å士ã§ãååéè¤ã¯è¨±ãããªãã®ã§ãããªã¢ã¼ãDBã®Aã¹ãã¼ãã®X表ã¨Bã¹ãã¼ãã®X表ãã使ãããã±ã¼ã¹ãªã©ãå¤é¨è¡¨ã®ååã¨ãã¦å¥åã§å®ç¾©ãã¦ãããªãã¦ã¯ãããªãã®ã§ãã
Oracleã ã£ãããäºåã«å®ç¾©ãã¦ããTNSæ¥ç¶èå¥åã§ããã¼ãã«åï¼ æ¥ç¶å ãã¨æ¸ããããã ãã§èå¥ã§ãã¦ããã¨ããã§ãããPostgreSQLã§ã¯ããã¯ãããªãã§ãã
ãããªãã¨ãè¸ã¾ããªãããï¼ã¤ï¼ã¤å¤é¨ãã¼ãã«ã®å®ç¾©ãä½ã£ã¦ãããã¨ã«ãªãã¾ãã
ï¼ãIMPORT FOREIGN SCHEMAãã§ããªã¢ã¼ãã®ã¹ãã¼ãé
ä¸ã®ãã¼ãã«å®ç¾©ãä¸çºã§ãã¼ã«ã«å´ã®å¤é¨è¡¨å®ç¾©ã¨ãã¦åãè¾¼ããã¨ãã§ãã¾ããååè¡çªããªããã°ãããï¼
ããã§ãããªãã¨ãªãfdwã®ã»ãã使ãããããã ãã¨æã£ã人ãå¤ãããããªãã§ããããã
å®éãfdwã®ã»ããé«éã ã¨ãè¨ããã¦ããããï¼èªåã§æ¸¬ã£ããã¨ã¯ãªãï¼
ãã ããfdwã¯ããã®ååãData Wrapperããããããéãããã¼ã¿ã¢ã¯ã»ã¹ã主ç¼ã¨ãã¦ãã¾ãã
ãã£ã¦ããªã¢ã¼ããµã¤ãã®ã¹ãã¢ãããã°ã©ã ãã³ã¼ã«ã§ããªãããã
ï¼ãã¼ãã«é¢æ°ä½¿ãã°ã§ããï¼ãã£ããã¨ãªããã©ãï¼
ãã®éã«ã¯ãdblinkã«é ¼ããããå¾ãªãã§ãã
çµå±ã©ã£ã¡ãã ããã£ã¦ããã«ã¯ãããªããã¨ã«ãªãã¾ãã
ãã¼ã¿ãã¼ã¹ãªã³ã¯ã¯ä½¿ãããã©-ãã®ï¼ï¼ãªã³ã¯ããã£ã¦ãOracleDBã¯ãåãã¿ã¤ãã³ã°ã§PostgreSQLã«ç§»è¡ãããã®ããï¼
OracleããPostgreSQLã¸ã®ç§»è¡è¨ç»ã®ãªãã§ãç½®ãã¦ãã¼ãã«ãã¦ãã¾ããã¼ã¿ãã¼ã¹ã¯ãã¾ãããï¼
åè¿°ã®fdwã®æ¨æºã¯ãPostgreSQLå士ã®ãªã¢ã¼ãã¢ã¯ã»ã¹ãå®ç¾ãããpostgres_fdwãã§ããã
PostgreSQLã«ç§»è¡ãããã¼ã¿ãã¼ã¹ãããç½®ãã¦ãã¼ãã«ãã¦ãã¾ã£ãOracleãåç §ããã«ã¯ãoracle_fdwãããã¾ããå¸æã®å ã§ããã
é·ããAWS RDS/Aurora PostgreSQLã§ãµãã¼ãããããã©ã¤æãããã¦ããããå æ¥ãããããµãã¼ããããããã«ãªãã¾ããããã°ãããã§ããã
念ã®çºããã£ããæ¤è¨¼ããªãã使ã£ã¦ãããã°è¯ãã¨æãã¾ãã
ãã ãããéåããã¯ã©ãã§ãããï¼ç½®ãå»ãã«ãããOracleãããå ã«ç§»è¡ãã¦ãã¾ã£ãPostgreSQLãåç §ããæ段ã¯ããã¾ããï¼
Generic Connectivityï¼Database Gatewayï¼ãããªã®ãããã¾ãããã
ä»æ´ãæ¬çªç¨¼åä¸ã®Oracleã«æ°ããã®å
¥ãããï¼ãããããã®DBãããã移è¡ããã¤ãããªãã§ããï¼ããã«è¿½å ã®ã³ã¹ãããããã¾ãï¼
追è¨ï¼2021/12/16
OracleããPostgreSQLã¸ã®Database Linkããè¨å®æ¹æ³ã®è¨äºãããã¾ããã®ã§ãªã³ã¯ãã¦ããã¾ãï¼
Oracle Database から PostgreSQL への接続を試す - Qiita
ä»ã«ãããããããããã©
ã¨ãããããããããã«ãã¦ããã¾ããæ¸ãã¦ãã¦ãã¤ããæãåºãããã£ã¦é ãçããªã£ã¦ãã¾ããã
ãã£ã¨è©³ããã移è¡ã®éã«æ¤è¨ãã¹ããã¨ãç¥ããã人ã«ã¯ãPostgreSQLã¨ã³ã¿ã¼ãã©ã¤ãºã³ã³ã½ã¼ã·ã¢ã ããå
¬éãã¦ããææç©ã®ããã¼ã¿ãã¼ã¹ç§»è¡ããèªãã§ã¿ã¦ãã ããã
PostgreSQL エンタープライズ・コンソーシアム : 成果物総索引
ãã®å£ä½ã®åãè ã§ã¯ãªãã§ãããPGEConsã®ãµã¤ãã«ã¯ããã®ããã¥ã¡ã³ãã«éããPostgreSQLé¢é£ã®é常ã«æç¨ãªã³ã³ãã³ããå ¬éããã¦ãã¾ãã
ããã«æ¸ããPostgreSQLã®ä»æ§ã¯ãããï¼ã¤ï¼ã¤ã®ä»æ§ã¨ãã¦ã¯ä½ä¸ä»£ãåããå¤ãããªããåºæ¬ä¸ã®åºæ¬ãã ã£ãããã¾ãã*1
æ®éã«PostgreSQLãåå¼·ãã¦ããããPostgreSQLå®å
¨ã«ç解ããã宣è¨ããããã«ã¯å¿
ãéã£ã¦ããã¯ããã¨ããã¬ãã«ã®ãã®ã§ãããªãããããã¬ãã«ã®è©±ã ã¨æãã¾ãã
ãã§ããããã«äººã ã¨ãä½åº¦ãè¦ã/èãã話ã§ãã¨ãã次å
ã§ã¯ãªããã¨ã
ã§ãããããªåºæ¬ä¸ã®åºæ¬ã®è©±ãããOracleãã移è¡ãã¨ããæèã«ä¹ã£ãé端ãå¶æ´ãªç´ é¡ãã®ãããã¾ãã
ããã§ãããã®é£é¢ãä¹ãè¶ããå¾ã«ã¯ãç´ æ´ãããPostgreSQLã©ã¤ããå¾ ã£ã¦ããã®ã§ãé å¼µã£ã¦ãã ããï¼
å®å¿ãã¦ãã ãããOracleããMySQLã«ä¹ãæããããã¯ãããããã©ã¯ã ããï¼
ï¼MySQLã«ã¯MySQLã®è¯ããããã¾ããã¿ãã·ã¤ã¨MySQLï¼ï¼
æå¾ã«
OracleããPostgreSQLã¸ã®ç§»è¡ã«æåããããã¸ã§ã¯ãã«é¢ãã£ãçããããã¸ã§ç´ æ´ãããã§ãã
ãã®çµé¨å¤ã®ãã¼ãºã¯é常ã«é«ãã®ã§ããã¾ããã£ãã¨ããã»ãã¾ããããªãã£ãã¨ããããã²å
¬éãã¦ã»ããã§ãã
Oracleã«æ®ã決æãããçããããããç´ æ´ãããã§ãããªãã ããã ã§æé«å³°ã®DBMSã ã¨æãã¾ãã
使ãã¨æ±ºãããååã«ä½¿ãåãã¾ãããã
ãã¤ãããã°ã¨ã³ããªã®æå¾ã«ãæ¨ãæ¬ããç´¹ä»ãã¦ã¾ãããä»åã¯ãPostgreSQLãéç¨ããä¸ã§çµ¶å¯¾ã«å½¹ã«ç«ã¤ãã¡ãã®æ¬ã§ã
*1:åºæ¬ä¸ã®åºæ¬ãã¨ãè¨ã£ã¦èªè誤ã£ã¦ãã¨ããããã£ããåªããæãã¦ä¸ããï¼