+ -
当前位置:首页 → 问答吧 → postgresql 创建表

postgresql 创建表

时间:2010-12-14

来源:互联网

linux 安装了postgresql8.4 ,
现在刚学postgresql,

[root@v1 tool]# su - postgres
-bash-3.2$ -bash-3.2$

-bash-3.2$ psql -l
                                 资料库列表
   名称    |  拥有者  | 字元编码 | 排序规则 | Ctype |       存取权限        
-----------+----------+----------+----------+-------+-----------------------
mydb      | postgres | UTF8     | C        | C     |   -----------》可以看见已经创建好的mydb数据库
postgres  | postgres | UTF8     | C        | C     |
template0 | postgres | UTF8     | C        | C     | =c/postgres
                                                    : postgres=CTc/postgres
template1 | postgres | UTF8     | C        | C     | =c/postgres
                                                    : postgres=CTc/postgres
test      | postgres | UTF8     | C        | C     |
(5 行记录)

下面我打算在数据库里,创建weather表,在表里添加己行数据
-bash-3.2$ psql mydb
psql (8.4.4)
输入 "help" 来获取帮助信息.

mydb=# COPY weather from '/tool/weather.txt'   
mydb-# INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27')
mydb-# SELECT * FROM weather   ---------------》没有任何显示和报错,不知道哪里问题 ?

[root@v1 tool]# cat /tool/weather.txt
city            varchar(80),
temp_lo         int,           -- 最低气温
temp_hi         int,           -- 最高气温
prcp            real,          -- 降水量
date            date

作者: dwm123   发布时间: 2010-12-14

不需要写insert语句,例如

mydb=# COPY weather from '/tool/weather.txt' ;

注意命令结尾加分号;

作者: tanfeng3   发布时间: 2010-12-14

不需要写insert语句,例如

mydb=# COPY weather from '/tool/weather.txt' ;

注意命令结尾加分号;

作者: tanfeng3   发布时间: 2010-12-14

不能加分号阿,如果加分号就报错。

作者: dwm123   发布时间: 2010-12-14

回复 tanfeng3

mydb=# COPY weather from '/tool/weather.txt';
ERROR:  relation "weather" does not exist

作者: dwm123   发布时间: 2010-12-14



QUOTE:
回复  tanfeng3

mydb=# COPY weather from '/tool/weather.txt';
ERROR:  relation "weather" does no ...
dwm123 发表于 2010-12-14 10:42




    weather  表建在哪个schema下? 加下前辍例如;

copy public.weather from '/tool/weather.txt';

作者: tanfeng3   发布时间: 2010-12-14

哥,还是多看些PG文档吧,8。4的文档里也要常看下。

作者: tanfeng3   发布时间: 2010-12-14

本帖最后由 dwm123 于 2010-12-14 11:30 编辑

回复 tanfeng3


我还是不太明白你的意思,我现在已经建立了一个数据库叫mydb, 现在只是想在mydb下面创建个weather的表,而且我现在已经在mydb的数据库里了。

mydb=# COPY mydb.weather from '/tool/weahter.txt' 如果不加分号,就不会报错,但是selcet * from weather 什么都不显示。

你说的加个前缀,我不是很明白,

mydb=# COPY mydb.weather from '/tool/weahter.txt'
ERROR:  schema "mydb" does not exist


-bash-3.2$ psql -l
                                 资料库列表
   名称    |  拥有者  | 字元编码 | 排序规则 | Ctype |       存取权限        
-----------+----------+----------+----------+-------+-----------------------
cities    | postgres | UTF8     | C        | C     |
mydb      | postgres | UTF8     | C        | C     |
postgres  | postgres | UTF8     | C        | C     |
template0 | postgres | UTF8     | C        | C     | =c/postgres
                                                    : postgres=CTc/postgres
template1 | postgres | UTF8     | C        | C     | =c/postgres
                                                    : postgres=CTc/postgres
test      | postgres | UTF8     | C        | C     |
(6 行记录)

我现在情况就这些,你能否直接点告诉我,在mydb里建立个weather 表,如何操作。

作者: dwm123   发布时间: 2010-12-14

很基本的知识。SQL语句用分号断句啊。

作者: renxiao2003   发布时间: 2010-12-14

回复 renxiao2003

-bash-3.2$ createdb abc  ---我这里加不加分号,都可以创建成功,我现在是想在mydb下面创建个表weather,
-bash-3.2$ pgsql -l


                                资料库列表
   名称    |  拥有者  | 字元编码 | 排序规则 | Ctype |       存取权限        
-----------+----------+----------+----------+-------+-----------------------
abc       | postgres | UTF8     | C        | C     |   ----------------------》刚创建
cities    | postgres | UTF8     | C        | C     |
mydb      | postgres | UTF8     | C        | C     |
postgres  | postgres | UTF8     | C        | C     |
template0 | postgres | UTF8     | C        | C     | =c/postgres
                                                    : postgres=CTc/postgres
template1 | postgres | UTF8     | C        | C     | =c/postgres
                                                    : postgres=CTc/postgres
test      | postgres | UTF8     | C        | C     |
(7 行记录)

作者: dwm123   发布时间: 2010-12-14

Hi, 如果想往weather 表 COPY数据,按照以下,把结果打印出来

1 连接 mydb 库
psql mydb postgres

2 查看表属性
\d weather

把第二步的结果打印出来

作者: tanfeng3   发布时间: 2010-12-14

Hi, 如果想往weather 表 COPY数据,按照以下,把结果打印出来

1 连接 mydb 库
psql mydb postgres

2 查看表属性
\d weather

把第二步的结果打印出来

作者: tanfeng3   发布时间: 2010-12-14