Chinaunix

标题: 请教一些PG 初级的问题 how to switch between databases? [打印本页]

作者: khandielas    时间: 2009-05-16 23:03
标题: 请教一些PG 初级的问题 how to switch between databases?
ubuntu 上面 安装了 PG 8.3

我按照这个link给的步骤 http://hocuspokus.net/2008/05/install-postgresql-on-ubuntu-804

$ sudo su postgres -c psql template1
template1=# ALTER USER postgres WITH PASSWORD 'password';
template1=# \q

That alters the password for within the database, now we need to do the same for the unix user ‘postgres’:

$ sudo passwd -d postgres
$ sudo su postgres -c passwd

Now enter the same password that you used previously.

Then, from here on in we can use both pgAdmin and command-line access (as the postgres user) to run the database server. But before you jump into pgAdmin we should set-up the PostgreSQL admin pack that enables better logging and monitoring within pgAdmin. Run the following at the command-line:

$ sudo su postgres -c psql < /usr/share/postgresql/8.3/contrib/adminpack.sql

后面的configure我没有作。现在的问题是我是不是只有一个叫postgres的user,我想用他login怎么作呢? 我是必须让Linux的login 变成 postgres,才能login?

ubuntu:~$ su - postgres
Password:
Added user postgres.

postgres@ubuntu:~$ psql -U postgres
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

postgres=# create database second_database;
CREATE DATABASE

问题1,我怎么能知道我有多少DATABASE 了? mySQL 用 show DATABASES, PG 里面什么命令能作,我查8.3的manual也没找到。

问题2,如果我不想用postgres去login,比如我想用khandielas这个user id, 那么是不是我必须要在Linux上面有个用户名叫 khandielas。登入PG,一定要用同样的Linux 和 PG 用户名?


第一次用PG的 菜鸟, 十分感谢大伙的帮助

另外我现在想作个免费的hospital information system, 不想用mysql因为医院对速度要求不高。考虑到以后的功能拓展,PG 应该是最好的。如果哪位PG高手有兴趣,愿意出点力的话,请联系我 free.his.cn@gmail.com
现在国内主流医院系统的都是用SQL server 和 oracle。

[ 本帖最后由 khandielas 于 2009-5-17 12:29 编辑 ]
作者: trainee    时间: 2009-05-17 08:08
问题0,操作系统的user和数据库的user没有任何关系。 操作系统建立一个postgres的用户是为了:
      a. 让数据库以postgres的用户权限启动,避免用超级用户启动服务器,以增加安全
      b. 让psql登陆简便化,直接输psql就可登陆,因为 如果没有加 ‘-u 数据库用户名’,它会将操作系统的用户来代替。

问提1,如果是SQL语句,请查阅pg_catalog模式中的系统表或view, 具体什么请看文档, 在psql中有简化的命令 \l

问题2,不必,原因如问题0
作者: postgres_fan    时间: 2009-05-17 12:18
标题: 回复 #1 khandielas 的帖子
查看
http://www.wohedb.com/db_html_do ... orial_1_install.htm

http://www.wohedb.com/db_html_do ... /adm_2_security.htm

对PostgreSQL的安全机制有详细说明。
作者: khandielas    时间: 2009-05-17 12:28
谢谢postgres_fan

如果有俩个database,怎么能从连接一个的时候换到另外一个去?

SQL server 可以用 use DATABASE_NAME; 就可以用另外一个database了,PG怎么弄?

我用\connect DATABASE_NAME 去连,可是我还要再次键入密码,这很不方便,有没有之间把密码一起键入的功能。

比如:

khandielas=# \connect MyApp
Password for user khandielas:

[ 本帖最后由 khandielas 于 2009-5-17 12:49 编辑 ]
作者: postgres_fan    时间: 2009-05-17 12:45
标题: 回复 #4 khandielas 的帖子
一个数据库连接只能访问一个数据库里面的内容,不能访问其它的数据库里的内容,所以不能切换到其它的数据库里面。 要想访问其它数据库里面的内容,必须建立一个新的数据库连接。
作者: khandielas    时间: 2009-05-17 12:51
原帖由 postgres_fan 于 2009-5-17 12:45 发表
一个数据库连接只能访问一个数据库里面的内容,不能访问其它的数据库里的内容,所以不能切换到其它的数据库里面。 要想访问其它数据库里面的内容,必须建立一个新的数据库连接。


回的真快,谢谢。 我刚刚改了上面的帖子,我用\connect 去连另外一个,可是还要键入密码,有没有方便的方法一个命令就搞定的?
作者: postgres_fan    时间: 2009-05-17 13:11
标题: 回复 #6 khandielas 的帖子
现在psql的\connet 命令不支持指定用户的密码。等我弄一个改进版的psql,把这个功能加进去。
作者: khandielas    时间: 2009-05-17 14:22
标题: 回复 #7 postgres_fan 的帖子
谢谢啊。不支持也可以,我在code里面直接用不同的用户名好了,反正只要能query出来就可以。

现在基本弄明白了
create user
create db
create table
alter table, query table 等等基本的东西了。

如果有问题我还要打扰你了,看了你是真高手




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