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=# \dtList of relationsSchema |  Name   | Type  |  Owner
--------+---------+-------+----------public | xx_user | table | postgres
(1 row)test=# \d xx_userTable "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-242 | bbb  | 15342525980 | 1980-01-013 | ccc  | 18761598031 | 1992-09-294 | ddd  | 15910909870 | 1990-09-215 | eee  | 15900909890 | 1990-02-26
(5 rows)

2、导出表结构

[postgres@server ~]$ 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 inheritance information
pg_dump: reading event triggers
pg_dump: finding extension tables
pg_dump: finding inheritance 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 inherited 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 CSTSET 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_seqAS integerSTART WITH 1INCREMENT BY 1NO MINVALUENO MAXVALUECACHE 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_userADD 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、导出数据

[postgres@server ~]$ 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文本文件中,数据列之间用逗号“,”分隔。

[postgres@server ~]$ 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_seqAS integerSTART WITH 1INCREMENT BY 1N' 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_userADD 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: 0mysql> 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相关推荐

  1. PostgreSQL导出表结构|表数据

    Windows PgAdmin 环境变量配置 PG_HOME:D:\Program Files\PostgreSQL\9.5 Path:%PG_HOME%\bin;%PG_HOME%\lib;%PG_ ...

  2. mysql命令导出表结构文件夹_mysql,命令导入\导出表结构或数据

    1.导出整个数据库 mysqldump -u用户名 -p密码  数据库名 > 导出的文件名 C:\Users\jack> mysqldump -uroot -pmysql db1  > ...

  3. oracle如何导出表结构及数据,PLSQL怎样导出oracle表结构和数据

    1.导出表结构和数据 方式1.tools->export user objects是导出表结构 tools ->export user object 选择选项,导出.sql文件 说明:导出 ...

  4. MySQL导出表结构表数据以及导入数据

    导出表结果表数据 使用mysqldump命令行下具体用法如下: mysqldump -u用户名 -p密码 -d 数据库名 表名 > 脚本名; 1.导出数据库为dbname的表结构(其中用户名ro ...

  5. mysql导入表结构命令是_mysql,命令导入\导出表结构或数据

    1.导出整个数据库 mysqldump -u用户名 -p密码  数据库名 > 导出的文件名 C:\Users\jack> mysqldump -uroot -pmysql db1  > ...

  6. mysql命令导出表结构和数据_mysql-用命令导出、导入表结构或数据

    1. 导出整个数据库(表结构和数据) mysqldump -u用户名 -p  数据库名 > 导出的文件名 [root@localhost work]# mysqldump -uroot -p m ...

  7. mysql导入dum_mysql,mysqldump命令导入 导出表结构或数据

    在命令行下mysql的数据导出有个很好用命令mysqldump,它的参数有一大把,可以这样查看: mysqldump 最常用的: mysqldump -uroot -pmysql databasefo ...

  8. front mysql 导出表结构_肿么将mysql的表结构导出到sqlserver中

    在命令行下mysql的数据导出有个很好用命令mysqldump,它的参数有一大把,可以这样查看: mysqldump 最常用的: mysqldump -uroot -pmysql databasefo ...

  9. mysql导出表结构及数据的三种方法

    navicat导出表一共有三中用法: 第一种:数据库上右键->"转储SQL文件",如图: "转储文件"是把整个数据库表全部导出,所有的表都是先drop然后 ...

最新文章

  1. qm'l 获取屏幕分辨率
  2. Go 语言 练习 聊天室 01
  3. ARM 之十 ARMCC(Keil) map 文件(映射文件)详解
  4. 01 | 顶层设计:微服务生态与 Spring Cloud Alibaba
  5. ios apple pay 证书配置
  6. 路径还原(求两个点之间最短距离的路径)
  7. 剑指offer系列之五十四:按之字形顺序打印二叉树
  8. 朋友圈虚拟点赞+评论在线生成[可选头像+赞数+时间+随机电量信号]
  9. Python 去除白色背景
  10. MeGUI 压片之新手上路
  11. 美多次透露加息 国债收益率持续走高
  12. java+js+html 实现webSocket广播及私聊
  13. 华为工作十年离职感想
  14. 12306的问题是技术难题吗
  15. 新书《完美统计图:Word/PPT/Excel数据可视化宝典》,包邮送
  16. 计算机一级插入页码,计算机一级WPS考试:WPS文字中页码插入及排版技巧
  17. python练习题——十大歌手
  18. 微前端在Vue项目的实践
  19. 认认真真写博客,踏踏实实编代码
  20. mplfinance绘制K线图

热门文章

  1. 基于Cookie-Editor与curl实现跨设备的文件下载
  2. 达梦数据库(DM8)基本使用
  3. 阿尔茨海默最新研究进展(2022年12月)
  4. Android与GNU体系
  5. #2021暑假杭电多校8_1003.Ink on paper
  6. 《安富莱嵌入式周报》第295期:世界杯球员和足球实时跟踪,开源手持矢量网络分析仪,自制柔性电容式传感器,IAR加强对VSCode支持、索尼早期PSX的光驱模拟器
  7. 前端谷歌浏览器基本介绍及前后端分离原理分析
  8. SpringMVC后缀
  9. using b tree mysql_浅析MysQL B-Tree 索引
  10. 计算机内存清理器,轻量便捷的内存清理工具PC版