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

2023-05-16

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、导出表结构

[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 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、导出数据

[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_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)

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

postgresql导出表结构以及数据到mysql 的相关文章

  • springboot+jpa+mongodb开发实战

    mongodb作为nosql数据库的一种 xff0c 在javaee开发中也越来越受到重视 xff0c 这里介绍springboot 43 jpa 43 mongodb开发过程 1 新建maven工程 xff0c 引入springboot和
  • hive查询结果保存到本地

    使用hive查询 xff0c 结果我们通常会保存到hdfs xff0c 然后通过sqoop导出工具 xff0c 将其导出到外部mysql或者其他地方 xff0c 但是有时候查询结果就是一个数据 xff0c 比如count 的结果 xff0c
  • spring-data-mongodb两种实现方式对比

    之前介绍过springboot和mongodb整合 xff0c 使用了spring data mongodb xff0c 因为springboot注解和自动化配置 xff0c 我们少了很多配置 xff0c 这里介绍spring整合mongo
  • shell脚本获取昨天今天本周周一本周周日本月第一天本月最后一天

    偶尔在linux上需要做脚本 xff0c 并设置脚本按照日期来计算相关数据 xff0c 这时候就涉及到日期的获取 xff0c 这里介绍简单的几个重要日期的获取 xff0c 分别是昨天 xff0c 今天是本周的第几天 xff0c 本周周一 x
  • 4399游戏存档的那些事儿

    4399游戏存档的那些事儿 本文旨在简单分析4399游戏存取档的加解密 工具在吾爱都有提供下载 工具名称用途ffdec反编译swf文件charles抓包开发IDE 随意 存档加解密复现 一 抓包 随意找一个支持存档的游戏进行抓包 这里不分析
  • mongodb数据库从入门到精通

    mongodb数据库作为nosql的一种 xff0c 目前在nosql数据库中的应用排名几乎是数一数二 开发中也会越来越受到重视 xff0c 这里介绍命令行下操作mongo数据库的相关内容 基础部分 连接数据库 xff1a 一般连接数据库
  • linux上安装svn

    1 安装 yum install subversion 2 测试是否成功及查看版本 svnserve version 3 创建目录 makedir p home hadoop workspace svn pro 4 创建svn目录 svna
  • java通过jedis操作redis(从JedisPool到JedisCluster)

    redis作为一个缓存数据库 xff0c 在绝大多数java项目开发中是必须使用的 xff0c 在web项目中 xff0c 直接配合spring redis xff0c 各种配置都直接在spring配置文件中做了 xff0c 一般都是使用r
  • Lua redis() command arguments must be strings or integers

    如题 xff0c 在linux命令行下运行lua脚本操作redis xff0c 提示错误 root 64 server script cat set lua return redis call 39 set 39 KEYS 1 ARGV 1
  • git命令行下回退一个文件到上一个版本

    git版本控制在ide中 xff0c 很方便的回退一个文件 xff0c 只需要git gt revert就可以了 但是有时候 xff0c 我们会在命令行下操作git 比如部署到生产环境的时候 xff0c 我们不想打包 xff0c 而是想通过
  • 让web页面页脚footer固定在页面底部

    有时候 xff0c 我们发现很多页面内容不多的时候 xff0c 页面底部内容飘到了中间 xff1a 这个页面底部没有固定 xff0c 结果 xff0c 一个前端工程师是无法接受这样的结果的 这里介绍一种通用的解决办法 xff0c 让页面底部
  • 图文演示第一个nodejs实例

    学习nodejs xff0c 第一个项目就是如何构建一个简单的http服务 xff0c 然后通过浏览器输入访问地址访问 xff0c 打印一个简单的helloworld 这里直接入手 xff0c 首先确保机器上已经安装了node npm 以及
  • 构建http服务的几种方式

    做前端开发 xff0c 页面可以通过浏览器打开访问 xff0c 但是异步请求这里 xff0c 直接通过浏览器就不行了 xff0c 即便是访问的本地json资源 xff0c ajax也无能为力 另外 xff0c 如angularjs vue涉
  • nodejs+Express开发第一个web应用

    express是nodejs开发中最常用的一个http服务框架 xff0c 通过他可以很方便的构建http服务 另外他本身还提供了路由功能 xff0c 就是对请求的路径做区分 xff0c 分别对应后台不同的请求 这里介绍如何通过expres
  • angularjs路由ui-router示例

    angularjs默认提供的路由是angular route min js提供的 xff0c 这里介绍一个开源的 xff0c 基于angularjs实现的ui router路由 思路是类似的 页面不再使用ng view来指定一个模板子页面
  • DZNEmptyDataSet框架简介

    给大家推荐一个设置页面加载失败时显示加载失败等的框架 下载地址 DZNEmptyDataSet https github com dzenbot DZNEmptyDataSet 上效果 首先在你的ViewController里面导入 imp
  • 使用jQuery中Deferred异步对象构建顺序执行队列

    在前面 xff0c 有一篇文章介绍了jQuery的异步对象Deferred xff0c 通过他 xff0c 我们可以实现一些操作比如回调函数在异步操作 xff08 耗时 xff09 完成之后再执行 比如这样的场景 xff0c 我们在编辑页面
  • nodejs路由之代码分离

    nodejs提供了路由功能 xff0c 解决客户端各类请求对应的处理问题 xff0c 相当于springboot开发各个controller对应的方法 xff0c nodejs提供的路由 xff0c 也支持各种方法 xff1a get po
  • gcc编译c++文件

    gcc是编译c语言的 xff0c 默认情况下 xff0c 如果直接编译c 43 43 程序 xff0c 会报错 xff1a root 64 server demo2 ls hello cpp root 64 server demo2 cat
  • linux下C++连接mysql查询数据

    windows下使用C 43 43 连接mysql相对繁琐 xff0c 这里直接在linux下通过C 43 43 连接mysql xff0c 执行查询操作 linux下连接mysql xff0c 需要本机有libmysqlclient库文件

随机推荐

  • linux下C++连接redis

    linux下c 43 43 连接redis 需要安装redis 同时还需要安装hiredis xff0c hiredis是c 43 43 操作redis的api库 redis的安装这里不介绍了 xff0c 很多教程 这里直接从hiredis
  • sqlite3简单入门

    linux上一般默认是自带了sqlite3的 xff0c 所以学习sqlite3 xff0c 最好使用linux 这样可以跳过安装这一步 开始 xff0c 我们可以直接运行sqlite3 testsqlite3 db databases 可
  • linux下C++连接sqlite3

    linux下 xff0c c 43 43 连接sqlite3 xff0c 不需要额外的安装依赖库 xff0c linux自带了sqlite3 xff0c 在编译的时候 xff0c 只需要将sqlite3的库加入编译参数中即可 sqlite3
  • mongodb查询两个字段做加减乘除操作

    和使用关系型数据库一样 xff0c 我们在使用mongodb的时候 xff0c 我们希望有这样的操作 xff0c 就是查询两个字段的乘积或者和 xff0c 这就需要用到聚合查询了 xff0c 聚合查询的语法大致如下 xff1a db use
  • mongodb副本集oplogSize设置过小的问题

    mongodb副本集构建的高可用方案 xff0c 最少需要三个节点 xff0c 一个主节点master xff0c 一个从节点slave xff0c 一个选举仲裁节点arbiter 当主节点奔溃的时候 xff0c 仲裁节点选举从节点来接替主
  • mongodb开启安全认证

    默认 xff0c mongodb不开启安全认证 xff0c 通过mongo shell访问 xff0c 我们会看到mongo shell报出警告 xff1a 后面两个警告很好解决 xff1a 按照提示 xff0c 我们修改 sys kern
  • CCF-201809-3-元素选择器

    题目很长 xff0c 大家自行去官网看 第三题还是一如既往的是大模拟 xff0c 模拟css元素选择器 xff0c 有接触过前端的同学对此不陌生了吧 以前学css的时候就想过层叠样式表的实现 xff0c 但是也没细究 ccf第三题有出过ma
  • hbase shell命令行下常见操作

    hbase是基于hadoop的列簇数据库 xff0c 是nosql的一种 当我们搭建了hbase环境之后 xff0c 可以通过hbase shell命令 xff0c 进入hbase的命令行下 xff0c 可以进行创建表 xff0c 添加数据
  • redis高可用sentinel哨兵模式环境搭建

    redis高可用需要保证 xff0c 在主节点崩溃的时候 xff0c 从节点能够成为主节点 xff0c 继续提供服务 默认来说主从模式master slave就能做到这一点 xff0c 但是在实际环境中 xff0c 客户端连接的是指定的主机
  • win7笔记本电脑设置WiFi热点

    一般情况下 xff0c 我们是利用路由器设置WiFi热点 xff0c 但是如果没有路由器 xff0c 而有网线 xff0c 我们可以利用笔记本电脑来设置WiFi热点 xff0c 这里介绍如何通过笔记本电脑连接网线设置WiFi热点 笔记本电脑
  • 脚本之家上的一道题:如何通过findstr查找80和443端口记录

    一般来说 xff0c 我们都是在linux上编写bash脚本 xff0c 如果是在windows系统上 xff0c 我们就需要编写批处理脚本bat来处理相关业务逻辑 脚本之家上有这么一道题 xff1a 需要找到80 443端口的记录 xff
  • scylladb:利用java api操作scylladb数据库

    博客上全是关于scylladb的介绍和性能对比 xff0c 很少有scylladb方面的干货 xff0c 这里从scylladb官网各种文档里面整合出一个java版的例子 xff0c 例子中展示如何连接scylladb xff0c 如何加载
  • scylladb:设置监听主机IP为非默认localhost

    前面介绍了通过scylladb提供的driver xff0c 编写java代码 xff0c 然后操作scylladb数据库 但是因为scylladb默认采用的监听地址是localhost即127 0 0 1 xff0c 如果想从外部连接sc
  • mysql修改记录时update 字段=字段+字符串

    在有些场景下 xff0c 我们需要对我们的varchar类型的字段做修改 xff0c 而修改的结果为两个字段的拼接或者一个字段 43 字符串的拼接 如下所示 xff0c 我们希望将xx role表中的name修改为name 43 id 在m
  • MySQL存储过程入门

    存储过程是一种在数据库中存储复杂程序 xff0c 以便外部程序调用的一种数据库对象 存储过程是为了完成特定功能的SQL语句集 xff0c 经编译创建并保存在数据库中 xff0c 用户可以通过指定存储过程的名字并给定参数来调用执行 存储过程思
  • MySQL批量插入优化

    前面介绍了MySQL批量插入可以通过存储过程的方式来实现 xff0c 这里介绍批量插入100W记录 xff0c 并做一个优化 建表语句 xff1a create table sql drop table if exists xx user
  • MySQL数据库演示内连接左外连接右外连接的区别

    sql中join的出现解决了from tableA a tableB b where a id 61 b id连接查询做笛卡尔积的问题 xff0c 而join连接查询除了条件使用关键字on取代了where之外 xff0c 他本身的连接又分为
  • Linux基础教程: 4、用户组和用户的创建

    其实在我们安装完成一个linux的系统过程中 xff0c 会需要我们输入用户名和密码 xff0c 据我所知 xff0c 这个时候会自动创建一个用户和一个以用户名命名的用户组 xff0c 但是奇怪的就是我们这个用户并没有在这个组下面他仅仅是创
  • postgresql主从复制配置

    postgresql主从复制是一种高可用解决方案 xff0c 可以实现读写分离 postgresql主从复制是基于xlog来实现的 xff0c 主库开启日志功能 xff0c 从库根据主库xlog来完成数据的同步 主从复需要注意的地方 xff
  • postgresql导出表结构以及数据到mysql

    postgresql导出的表结构在语句上会和mysql有些差异 xff0c 因此当我们在mysql命令行中执行的时候 xff0c 会有警告和错误提示 xff0c 但是最终还是会将表生成成功 xff0c 这里将表结构和数据分别单独导出 xff