用COPY命令从csv文件中导入数据

本文详细介绍如何将CSV文件的数据导入到PostgreSQL数据库中,并通过实际案例演示了创建表、导入数据、分析表等操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始

csv 文件的内容:

id    name    departno    age
1    gao    10    30
2    jian    11    35
3    tom    11    30

导入前:

postgres=# select a.relpages, a.reltuples, a.relfilenode,a.reltype,b.typname from pg_class a, pg_type b where a.relname like 'gaotab%' and a.reltype=b.oid;
 relpages | reltuples | relfilenode | reltype | typname 
----------+-----------+-------------+---------+---------
(0 rows)

postgres=# 

导入之前,必须要建立好表的结构

postgres=# create table gaotab(id integer,name varchar(20),departno integer,age integer);
CREATE TABLE
postgres=# 
postgres=# 
postgres=# COPY gaotab from '/soft/test.csv' with csv header;COPY 3
postgres=# select * from gaotab;
 id | name | departno | age 
----+------+----------+-----
  1 | gao  |       10 |  30
  2 | jian |       11 |  35
  3 | tom  |       11 |  30
(3 rows)

postgres=# 

导入已经成功

导入后再看:

postgres=# select a.relpages, a.reltuples, a.relfilenode,a.reltype,b.typname from pg_class a, pg_type b where a.relname like 'gaotab%' and a.reltype=b.oid;
 relpages | reltuples | relfilenode | reltype | typname 
----------+-----------+-------------+---------+---------
        0 |         0 |       16384 |   16386 | gaotab
(1 row)

postgres=# 



postgres=# analyze gaotab;
ANALYZE
postgres=# select a.relpages, a.reltuples, a.relfilenode,a.reltype,b.typname from pg_class a, pg_type b where a.relname like 'gaotab%' and a.reltype=b.oid;
 relpages | reltuples | relfilenode | reltype | typname 
----------+-----------+-------------+---------+---------
        1 |         3 |       16384 |   16386 | gaotab
(1 row)

postgres=# 

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

结束

转载于:https://www.cnblogs.com/gaojian/archive/2012/11/07/2758118.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值