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