Chinaunix

标题: postgresql 创建表 [打印本页]

作者: dwm123    时间: 2010-12-14 10:28
标题: postgresql 创建表
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
作者: tanfeng3    时间: 2010-12-14 10:34
不需要写insert语句,例如

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

注意命令结尾加分号;
作者: tanfeng3    时间: 2010-12-14 10:34
不需要写insert语句,例如

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

注意命令结尾加分号;
作者: tanfeng3    时间: 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';
作者: tanfeng3    时间: 2010-12-14 10:57
哥,还是多看些PG文档吧,8。4的文档里也要常看下。
作者: dwm123    时间: 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 表,如何操作。
作者: renxiao2003    时间: 2010-12-14 11:29
很基本的知识。SQL语句用分号断句啊。
作者: dwm123    时间: 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 行记录)
作者: tanfeng3    时间: 2010-12-14 11:45
Hi, 如果想往weather 表 COPY数据,按照以下,把结果打印出来

1 连接 mydb 库
psql mydb postgres

2 查看表属性
\d weather

把第二步的结果打印出来
作者: tanfeng3    时间: 2010-12-14 11:46
本帖最后由 tanfeng3 于 2010-12-14 12:31 编辑

to dwm123 :

              schema和DB是不同的概念,如果你了解Oracle, 应该对 schema 不陌生。
作者: tanfeng3    时间: 2010-12-14 12:29
本帖最后由 tanfeng3 于 2010-12-14 12:30 编辑

又重复了
作者: dwm123    时间: 2010-12-14 15:44
本帖最后由 dwm123 于 2010-12-14 16:34 编辑

回复 11# tanfeng3


   多谢了,

[root@v1 ~]# service postgresql status
(pid  10635) 正在运行...
[root@v1 ~]# su - postgres
-bash-3.2$ psql mydb postgres
psql (8.4.4)
输入 "help" 来获取帮助信息.

mydb=# \d weather
         资料表 "public.weather"
  栏位   |         型别          | 修饰词
---------+-----------------------+--------
city    | character varying(80) |
temp_lo | integer               |
temp_hi | integer               |
prcp    | real                  |
date    | date                  |

mydb=# INSERT INTO weather (city,temp_lo,temp_hi,prcp,date) VALUES ('san francisco',43,57,0.0,'2010-01-01');
INSERT 0 1
mydb=# SELECT * from weather ;
     city      | temp_lo | temp_hi | prcp |    date   
---------------+---------+---------+------+------------
san francisco |      43 |      57 |    0 | 2010-01-01
(1 行记录)

mydb=# SELECT * from weather;
     city      | temp_lo | temp_hi | prcp |    date   
---------------+---------+---------+------+------------
san francisco |      43 |      57 |    0 | 2010-01-01
(1 行记录)

这样应该可以了。多谢了。可能我上午那里弄错了。


如果我在其他机器上想连接postgresql数据库(192.168.1.85),客户系统所linux,需要在客户机上运行什么命令。
作者: tanfeng3    时间: 2010-12-14 19:20
psql -h ip_address -p port -d db_name -U user_name
这样就行,
建议多看文档,多做实验。
作者: renxiao2003    时间: 2010-12-14 19:57
是的。看看表创建上没有啊。
作者: dwm123    时间: 2010-12-14 21:49
回复 13# tanfeng3


    我在一台linux客户机上连接数据库,拒绝,找了一下文档,说是按照下面的方法,可是tcpip_socket这个选项在postgresql.conf,不存在,是需要另外添加,还是我少装了东西。

<1>要使Linux上的PostgreSQL打开 “unix的tcpip套接子”。
编辑 $POSTGRES/data/postgresql.conf 文件,
将tcpip_socket=off改成tcpip_socket=on即可




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2