免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9000 | 回复: 14
打印 上一主题 下一主题

postgresql 创建表 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-14 10:28 |只看该作者 |倒序浏览
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

论坛徽章:
4
丑牛
日期:2014-09-15 09:28:12双子座
日期:2014-12-19 17:01:192015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2016-06-28 06:20:00
2 [报告]
发表于 2010-12-14 10:34 |只看该作者
不需要写insert语句,例如

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

注意命令结尾加分号;

论坛徽章:
4
丑牛
日期:2014-09-15 09:28:12双子座
日期:2014-12-19 17:01:192015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2016-06-28 06:20:00
3 [报告]
发表于 2010-12-14 10:34 |只看该作者
不需要写insert语句,例如

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

注意命令结尾加分号;

论坛徽章:
4
丑牛
日期:2014-09-15 09:28:12双子座
日期:2014-12-19 17:01:192015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2016-06-28 06:20:00
4 [报告]
发表于 2010-12-14 10:55 |只看该作者
回复  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';

论坛徽章:
4
丑牛
日期:2014-09-15 09:28:12双子座
日期:2014-12-19 17:01:192015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2016-06-28 06:20:00
5 [报告]
发表于 2010-12-14 10:57 |只看该作者
哥,还是多看些PG文档吧,8。4的文档里也要常看下。

论坛徽章:
0
6 [报告]
发表于 2010-12-14 11:28 |只看该作者
本帖最后由 dwm123 于 2010-12-14 11:30 编辑

回复 6# 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 表,如何操作。

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
7 [报告]
发表于 2010-12-14 11:29 |只看该作者
很基本的知识。SQL语句用分号断句啊。

论坛徽章:
0
8 [报告]
发表于 2010-12-14 11:32 |只看该作者
回复 9# 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 行记录)

论坛徽章:
4
丑牛
日期:2014-09-15 09:28:12双子座
日期:2014-12-19 17:01:192015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2016-06-28 06:20:00
9 [报告]
发表于 2010-12-14 11:45 |只看该作者
Hi, 如果想往weather 表 COPY数据,按照以下,把结果打印出来

1 连接 mydb 库
psql mydb postgres

2 查看表属性
\d weather

把第二步的结果打印出来

论坛徽章:
4
丑牛
日期:2014-09-15 09:28:12双子座
日期:2014-12-19 17:01:192015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2016-06-28 06:20:00
10 [报告]
发表于 2010-12-14 11:46 |只看该作者
本帖最后由 tanfeng3 于 2010-12-14 12:31 编辑

to dwm123 :

              schema和DB是不同的概念,如果你了解Oracle, 应该对 schema 不陌生。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP