当前位置:   article > 正文

postgresql导出表结构以及数据到mysql_pg 导出表结构

pg 导出表结构

postgresql导出的表结构在语句上会和mysql有些差异,因此当我们在mysql命令行中执行的时候,会有警告和错误提示,但是最终还是会将表生成成功,这里将表结构和数据分别单独导出,而且使用的语法和方法都不一样。

导出表结构直接使用postgresql命令pg_dump,而导出数据使用psql命令的copy。在mysql中导入表结构,我们执行source /path/to/table.sql,我们导入的表数据是单独的,而且是格式化的数据,我们通过load data local infile语句导入,需要指定列分隔符,以及行分隔符。

1、检查表结构和数据

  1. postgres=# \c test
  2. You are now connected to database "test" as user "postgres".
  3. test=# \dt
  4. List of relations
  5. Schema | Name | Type | Owner
  6. --------+---------+-------+----------
  7. public | xx_user | table | postgres
  8. (1 row)
  9. test=# \d xx_user
  10. Table "public.xx_user"
  11. Column | Type | Collation | Nullable | Default
  12. --------+-----------------------+-----------+----------+-------------------------------------
  13. id | integer | | not null | nextval('xx_user_id_seq'::regclass)
  14. name | character varying(20) | | |
  15. mobile | character varying(20) | | |
  16. birth | date | | |
  17. Indexes:
  18. "xx_user_pkey" PRIMARY KEY, btree (id)
  19. test=# select * from xx_user;
  20. id | name | mobile | birth
  21. ----+------+-------------+------------
  22. 1 | aaa | 13886604139 | 1987-08-24
  23. 2 | bbb | 15342525980 | 1980-01-01
  24. 3 | ccc | 18761598031 | 1992-09-29
  25. 4 | ddd | 15910909870 | 1990-09-21
  26. 5 | eee | 15900909890 | 1990-02-26
  27. (5 rows)

2、导出表结构

  1. [postgres@server ~]$ pg_dump --verbose --schema-only --table=xx_user --db=test --file=/home/postgres/user.sql
  2. pg_dump: last built-in OID is 16383
  3. pg_dump: reading extensions
  4. pg_dump: identifying extension members
  5. pg_dump: reading schemas
  6. pg_dump: reading user-defined tables
  7. pg_dump: reading user-defined functions
  8. pg_dump: reading user-defined types
  9. pg_dump: reading procedural languages
  10. pg_dump: reading user-defined aggregate functions
  11. pg_dump: reading user-defined operators
  12. pg_dump: reading user-defined access methods
  13. pg_dump: reading user-defined operator classes
  14. pg_dump: reading user-defined operator families
  15. pg_dump: reading user-defined text search parsers
  16. pg_dump: reading user-defined text search templates
  17. pg_dump: reading user-defined text search dictionaries
  18. pg_dump: reading user-defined text search configurations
  19. pg_dump: reading user-defined foreign-data wrappers
  20. pg_dump: reading user-defined foreign servers
  21. pg_dump: reading default privileges
  22. pg_dump: reading user-defined collations
  23. pg_dump: reading user-defined conversions
  24. pg_dump: reading type casts
  25. pg_dump: reading transforms
  26. pg_dump: reading table inheritance information
  27. pg_dump: reading event triggers
  28. pg_dump: finding extension tables
  29. pg_dump: finding inheritance relationships
  30. pg_dump: reading column info for interesting tables
  31. pg_dump: finding the columns and types of table "public.xx_user"
  32. pg_dump: finding default expressions of table "public.xx_user"
  33. pg_dump: flagging inherited columns in subtables
  34. pg_dump: reading indexes
  35. pg_dump: reading indexes for table "public.xx_user"
  36. pg_dump: flagging indexes in partitioned tables
  37. pg_dump: reading extended statistics
  38. pg_dump: reading constraints
  39. pg_dump: reading triggers
  40. pg_dump: reading rewrite rules
  41. pg_dump: reading policies
  42. pg_dump: reading row security enabled for table "public.xx_user_id_seq"
  43. pg_dump: reading policies for table "public.xx_user_id_seq"
  44. pg_dump: reading row security enabled for table "public.xx_user"
  45. pg_dump: reading policies for table "public.xx_user"
  46. pg_dump: reading publications
  47. pg_dump: reading publication membership
  48. pg_dump: reading publication membership for table "public.xx_user"
  49. pg_dump: reading subscriptions
  50. pg_dump: reading dependency data
  51. pg_dump: saving encoding = UTF8
  52. pg_dump: saving standard_conforming_strings = on
  53. pg_dump: saving search_path =
  54. pg_dump: creating TABLE "public.xx_user"
  55. pg_dump: creating SEQUENCE "public.xx_user_id_seq"
  56. pg_dump: creating SEQUENCE OWNED BY "public.xx_user_id_seq"
  57. pg_dump: creating DEFAULT "public.xx_user id"
  58. pg_dump: creating CONSTRAINT "public.xx_user xx_user_pkey"

我们可以看看生成的sql语句:

  1. --
  2. -- PostgreSQL database dump
  3. --
  4. -- Dumped from database version 11.4
  5. -- Dumped by pg_dump version 11.4
  6. -- Started on 2019-07-21 08:33:09 CST
  7. SET statement_timeout = 0;
  8. SET lock_timeout = 0;
  9. SET idle_in_transaction_session_timeout = 0;
  10. SET client_encoding = 'UTF8';
  11. SET standard_conforming_strings = on;
  12. SELECT pg_catalog.set_config('search_path', '', false);
  13. SET check_function_bodies = false;
  14. SET xmloption = content;
  15. SET client_min_messages = warning;
  16. SET row_security = off;
  17. SET default_tablespace = '';
  18. SET default_with_oids = false;
  19. --
  20. -- TOC entry 197 (class 1259 OID 16387)
  21. -- Name: xx_user; Type: TABLE; Schema: public; Owner: postgres
  22. --
  23. CREATE TABLE public.xx_user (
  24. id integer NOT NULL,
  25. name character varying(20),
  26. mobile character varying(20),
  27. birth date
  28. );
  29. ALTER TABLE public.xx_user OWNER TO postgres;
  30. --
  31. -- TOC entry 196 (class 1259 OID 16385)
  32. -- Name: xx_user_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
  33. --
  34. CREATE SEQUENCE public.xx_user_id_seq
  35. AS integer
  36. START WITH 1
  37. INCREMENT BY 1
  38. NO MINVALUE
  39. NO MAXVALUE
  40. CACHE 1;
  41. ALTER TABLE public.xx_user_id_seq OWNER TO postgres;
  42. --
  43. -- TOC entry 3086 (class 0 OID 0)
  44. -- Dependencies: 196
  45. -- Name: xx_user_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
  46. --
  47. ALTER SEQUENCE public.xx_user_id_seq OWNED BY public.xx_user.id;
  48. --
  49. -- TOC entry 2957 (class 2604 OID 16390)
  50. -- Name: xx_user id; Type: DEFAULT; Schema: public; Owner: postgres
  51. --
  52. ALTER TABLE ONLY public.xx_user ALTER COLUMN id SET DEFAULT nextval('public.xx_user_id_seq'::regclass);
  53. --
  54. -- TOC entry 2959 (class 2606 OID 16392)
  55. -- Name: xx_user xx_user_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
  56. --
  57. ALTER TABLE ONLY public.xx_user
  58. ADD CONSTRAINT xx_user_pkey PRIMARY KEY (id);
  59. -- Completed on 2019-07-21 08:33:09 CST
  60. --
  61. -- PostgreSQL database dump complete
  62. --

这里使用的是postgresql11.4,通过pg_dump导出表结构,从语句上看到,我们生成的表前面带有前缀"public.",这个前缀如果在mysql中执行会报错,因此我们需要将"public."这个前缀在sql文件中给去掉,暂时没有找到如何在导出的时候去掉这个前缀:“public.”,因此只能手工处理一下。 

 3、导出数据

  1. [postgres@server ~]$ psql
  2. psql (11.4)
  3. Type "help" for help.
  4. postgres=# \c test
  5. You are now connected to database "test" as user "postgres".
  6. test=# copy xx_user to '/home/postgres/user.txt' with (delimiter ',');
  7. COPY 5
  8. test=#

导出5条记录,我们将导出的数据保存在/home/postgres/user.txt文本文件中,数据列之间用逗号“,”分隔。

  1. [postgres@server ~]$ cat user.txt
  2. 1,aaa,13886604139,1987-08-24
  3. 2,bbb,15342525980,1980-01-01
  4. 3,ccc,18761598031,1992-09-29
  5. 4,ddd,15910909870,1990-09-21
  6. 5,eee,15900909890,1990-02-26

 4、在mysql中导入表结构:

  1. mysql> source /home/postgres/user.sql
  2. ERROR 1193 (HY000): Unknown system variable 'statement_timeout'
  3. ERROR 1193 (HY000): Unknown system variable 'lock_timeout'
  4. ERROR 1193 (HY000): Unknown system variable 'idle_in_transaction_session_timeout'
  5. ERROR 1193 (HY000): Unknown system variable 'client_encoding'
  6. ERROR 1193 (HY000): Unknown system variable 'standard_conforming_strings'
  7. ERROR 1305 (42000): FUNCTION pg_catalog.set_config does not exist
  8. ERROR 1193 (HY000): Unknown system variable 'check_function_bodies'
  9. ERROR 1193 (HY000): Unknown system variable 'xmloption'
  10. ERROR 1193 (HY000): Unknown system variable 'client_min_messages'
  11. ERROR 1193 (HY000): Unknown system variable 'row_security'
  12. ERROR 1193 (HY000): Unknown system variable 'default_with_oids'
  13. Query OK, 0 rows affected (0.02 sec)
  14. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SEQUENCE xx_user_id_seq
  15. AS integer
  16. START WITH 1
  17. INCREMENT BY 1
  18. N' at line 1
  19. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SEQUENCE xx_user_id_seq OWNED BY xx_user.id' at line 1
  20. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'xx_user ALTER COLUMN id SET DEFAULT nextval('xx_user_id_seq'::regclass)' at line 1
  21. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'public.xx_user
  22. ADD CONSTRAINT xx_user_pkey PRIMARY KEY (id)' at line 1
  23. mysql> desc xx_user;
  24. +--------+-------------+------+-----+---------+-------+
  25. | Field | Type | Null | Key | Default | Extra |
  26. +--------+-------------+------+-----+---------+-------+
  27. | id | int(11) | NO | | NULL | |
  28. | name | varchar(20) | YES | | NULL | |
  29. | mobile | varchar(20) | YES | | NULL | |
  30. | birth | date | YES | | NULL | |
  31. +--------+-------------+------+-----+---------+-------+
  32. 4 rows in set (0.05 sec)

这一步需要注意的是,我们在导出表结构的时候说过的问题,因为sql文件中,表名前面会带有"public."这个前缀,因此需要人为去掉,否则导入表结构会出现错误。 

5、在mysql中导入数据

  1. mysql> load data local infile '/home/postgres/user.txt' into table xx_user fields terminated by ',' lines terminated by '\n';
  2. Query OK, 5 rows affected (0.02 sec)
  3. Records: 5 Deleted: 0 Skipped: 0 Warnings: 0
  4. mysql> select * from xx_user;
  5. +----+------+-------------+------------+
  6. | id | name | mobile | birth |
  7. +----+------+-------------+------------+
  8. | 1 | aaa | 13886604139 | 1987-08-24 |
  9. | 2 | bbb | 15342525980 | 1980-01-01 |
  10. | 3 | ccc | 18761598031 | 1992-09-29 |
  11. | 4 | ddd | 15910909870 | 1990-09-21 |
  12. | 5 | eee | 15900909890 | 1990-02-26 |
  13. +----+------+-------------+------------+
  14. 5 rows in set (0.00 sec)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/894946
推荐阅读
相关标签
  

闽ICP备14008679号