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

postgresql导出表结构以及数据到mysql,第1张

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

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

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

1、检查表结构和数据

postgres=# \c test
You are Now connected to database "test" as user "postgres".
test=# \dt
List of relations
Schema | name | Type | Owner
--------+---------+-------+----------
public | xx_user | table | postgres
(1 row)

test=# \d xx_user
table "public.xx_user"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+-------------------------------------
ID | integer | | not null | nextval(‘xx_user_ID_seq‘::regclass)
name | character varying(20) | | |
mobile | character varying(20) | | |
birth | date | | |
Indexes:
"xx_user_pkey" PRIMARY KEY,btree (ID)

test=# select * from xx_user;
ID | name | mobile | birth
----+------+-------------+------------
1 | aaa | 13886604139 | 1987-08-24
2 | bbb | 15342525980 | 1980-01-01
3 | ccc | 18761598031 | 1992-09-29
4 | ddd | 15910909870 | 1990-09-21
5 | eee | 15900909890 | 1990-02-26
(5 rows)
2、导出表结构

[[email protected] ~]$ pg_dump --verbose --schema-only --table=xx_user --db=test --file=/home/postgres/user.sql
pg_dump: last built-in OID is 16383
pg_dump: reading extensions
pg_dump: IDentifying extension members
pg_dump: reading schemas
pg_dump: reading user-defined tables
pg_dump: reading user-defined functions
pg_dump: reading user-defined types
pg_dump: reading procedural languages
pg_dump: reading user-defined aggregate functions
pg_dump: reading user-defined operators
pg_dump: reading user-defined access methods
pg_dump: reading user-defined operator classes
pg_dump: reading user-defined operator familIEs
pg_dump: reading user-defined text search parsers
pg_dump: reading user-defined text search templates
pg_dump: reading user-defined text search dictionarIEs
pg_dump: reading user-defined text search configurations
pg_dump: reading user-defined foreign-data wrappers
pg_dump: reading user-defined foreign servers
pg_dump: reading default privileges
pg_dump: reading user-defined collations
pg_dump: reading user-defined conversions
pg_dump: reading type casts
pg_dump: reading transforms
pg_dump: reading table @R_419_5107@ance information
pg_dump: reading event triggers
pg_dump: finding extension tables
pg_dump: finding @R_419_5107@ance relationships
pg_dump: reading column info for interesting tables
pg_dump: finding the columns and types of table "public.xx_user"
pg_dump: finding default Expressions of table "public.xx_user"
pg_dump: flagging @R_419_5107@ed columns in subtables
pg_dump: reading indexes
pg_dump: reading indexes for table "public.xx_user"
pg_dump: flagging indexes in partitioned tables
pg_dump: reading extended statistics
pg_dump: reading constraints
pg_dump: reading triggers
pg_dump: reading rewrite rules
pg_dump: reading policIEs
pg_dump: reading row security enabled for table "public.xx_user_ID_seq"
pg_dump: reading policIEs for table "public.xx_user_ID_seq"
pg_dump: reading row security enabled for table "public.xx_user"
pg_dump: reading policIEs for table "public.xx_user"
pg_dump: reading publications
pg_dump: reading publication membership
pg_dump: reading publication membership for table "public.xx_user"
pg_dump: reading subscriptions
pg_dump: reading dependency data
pg_dump: saving enCoding = UTF8
pg_dump: saving standard_conforming_strings = on
pg_dump: saving search_path =
pg_dump: creating table "public.xx_user"
pg_dump: creating SEQUENCE "public.xx_user_ID_seq"
pg_dump: creating SEQUENCE OWNED BY "public.xx_user_ID_seq"
pg_dump: creating DEFAulT "public.xx_user ID"
pg_dump: creating CONSTRAINT "public.xx_user xx_user_pkey"
我们可以看看生成的SQL语句:

--
-- Postgresql database dump
--

-- Dumped from database version 11.4
-- Dumped by pg_dump version 11.4

-- Started on 2019-07-21 08:33:09 CST

SET statement_timeout = 0;
SET lock_timeout = 0;
SET IDle_in_transaction_session_timeout = 0;
SET clIEnt_enCoding = ‘UTF8‘;
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config(‘search_path‘,‘‘,false);
SET check_function_bodIEs = false;
SET xmloption = content;
SET clIEnt_min_messages = warning;
SET row_security = off;

SET default_tablespace = ‘‘;

SET default_with_oIDs = false;

--
-- TOC entry 197 (class 1259 OID 16387)
-- name: xx_user; Type: table; Schema: public; Owner: postgres
--

CREATE table public.xx_user (
ID integer NOT NulL,
name character varying(20),
mobile character varying(20),
birth date
);


ALTER table public.xx_user OWNER TO postgres;

--
-- TOC entry 196 (class 1259 OID 16385)
-- name: xx_user_ID_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--

CREATE SEQUENCE public.xx_user_ID_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


ALTER table public.xx_user_ID_seq OWNER TO postgres;

--
-- TOC entry 3086 (class 0 OID 0)
-- DependencIEs: 196
-- name: xx_user_ID_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--

ALTER SEQUENCE public.xx_user_ID_seq OWNED BY public.xx_user.ID;


--
-- TOC entry 2957 (class 2604 OID 16390)
-- name: xx_user ID; Type: DEFAulT; Schema: public; Owner: postgres
--

ALTER table ONLY public.xx_user ALTER ColUMN ID SET DEFAulT nextval(‘public.xx_user_ID_seq‘::regclass);


--
-- TOC entry 2959 (class 2606 OID 16392)
-- name: xx_user xx_user_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER table ONLY public.xx_user
ADD CONSTRAINT xx_user_pkey PRIMARY KEY (ID);


-- Completed on 2019-07-21 08:33:09 CST

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

 3、导出数据

[@L_502_1@ ~]$ psql
psql (11.4)
Type "help" for help.
postgres=# \c test
You are Now connected to database "test" as user "postgres".
test=# copy xx_user to ‘/home/postgres/user.txt‘ with (delimiter ‘,‘);
copY 5
test=#
导出5条记录,我们将导出的数据保存在/home/postgres/user.txt文本文件中,数据列之间用逗号“,”分隔。

[[email protected] ~]$ cat user.txt
1,aaa,13886604139,1987-08-24
2,bbb,15342525980,1980-01-01
3,ccc,18761598031,1992-09-29
4,ddd,15910909870,1990-09-21
5,eee,15900909890,1990-02-26
 4、在MysqL中导入表结构:

MysqL> source /home/postgres/user.sql
ERROR 1193 (HY000): UnkNown system variable ‘statement_timeout‘
ERROR 1193 (HY000): UnkNown system variable ‘lock_timeout‘
ERROR 1193 (HY000): UnkNown system variable ‘IDle_in_transaction_session_timeout‘
ERROR 1193 (HY000): UnkNown system variable ‘clIEnt_enCoding‘
ERROR 1193 (HY000): UnkNown system variable ‘standard_conforming_strings‘
ERROR 1305 (42000): FUNCTION pg_catalog.set_config does not exist
ERROR 1193 (HY000): UnkNown system variable ‘check_function_bodIEs‘
ERROR 1193 (HY000): UnkNown system variable ‘xmloption‘
ERROR 1193 (HY000): UnkNown system variable ‘clIEnt_min_messages‘
ERROR 1193 (HY000): UnkNown system variable ‘row_security‘
ERROR 1193 (HY000): UnkNown system variable ‘default_with_oIDs‘
query OK,0 rows affected (0.02 sec)

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
AS integer
START WITH 1
INCREMENT BY 1
N‘ at line 1
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
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
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
ADD CONSTRAINT xx_user_pkey PRIMARY KEY (ID)‘ at line 1
MysqL> desc xx_user;
+--------+-------------+------+-----+---------+-------+
| FIEld | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| ID | int(11) | NO | | NulL | |
| name | varchar(20) | YES | | NulL | |
| mobile | varchar(20) | YES | | NulL | |
| birth | date | YES | | NulL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.05 sec)
这一步需要注意的是,我们在导出表结构的时候说过的问题,因为sql文件中,表名前面会带有"public."这个前缀,因此需要人为去掉,否则导入表结构会出现错误。 

5、在MysqL中导入数据

MysqL> load data local infile ‘/home/postgres/user.txt‘ into table xx_user fIElds terminated by ‘,‘ lines terminated by ‘\n‘;query OK,5 rows affected (0.02 sec)Records: 5 Deleted: 0 Skipped: 0 Warnings: 0 MysqL> select * from xx_user;+----+------+-------------+------------+| ID | name | mobile | birth |+----+------+-------------+------------+| 1 | aaa | 13886604139 | 1987-08-24 || 2 | bbb | 15342525980 | 1980-01-01 || 3 | ccc | 18761598031 | 1992-09-29 || 4 | ddd | 15910909870 | 1990-09-21 || 5 | eee | 15900909890 | 1990-02-26 |+----+------+-------------+------------+5 rows in set (0.00 sec) ---------------------

总结

以上是内存溢出为你收集整理的postgresql导出表结构以及数据到mysql全部内容,希望文章能够帮你解决postgresql导出表结构以及数据到mysql所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1155672.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存