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

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-d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值