CentOS 6.2 上 PostgreSQL 9.1 的使用


前一篇文章()在用yum安装好postgresql之后,就可以通过service来管理它了。这个postgresql-9.1 的位置在

/etc/rc.d/init.d/postgresql-9.1

可以看到它里面默认使用了一个名为postgres的OS用户名(从这个脚本可以看到很多yum在安装postgresql后的目录位置、环境变量等)。这个postgres用户名就是(http://www.postgresql.org/docs/9.1/static/database-roles.html)这里提到的

In order to bootstrap the database system, a freshly initialized system always contains one predefined role. This role is always a "superuser", and by default (unless altered when running initdb) it will have the same name as the operating system user that initialized the database cluster. Customarily, this role will be named postgres. In order to create more roles you first have to connect as this initial role.

也就是说,系统会默认创建一个role,而且这个role在不指定(可以通过initdb指定)的情况下,它的name和当前初始化数据库实例的OS用户的name一样的。一般这个name就是postgres。从/etc/rc.d/init.d/postgresql-9.1里面也可以看到,脚本会把很多文件和文件夹的owner设置为postgres。

所以当你刚安装好postgres,迫不及待的想通过psql连接数据库的时候都会遇到(http://www.postgresql.org/docs/9.1/static/tutorial-createdb.html)这里提到的错误。因为psql认为你要使用当前的OS用户的name去连接数据库。错误如下:

createdb: could not connect to database postgres: FATAL:  role "joe" does not exist

当然,如果你自己的OS用户的name就是postgres,那你运气就太好了。:D

所以你需要

su - postgres

来切换到postgres用户下去,然后一个简单的:

psql

就可以顺利的连接到数据库了。

然后\password 可以修改你的密码。

在postgres下面

createuser root

切换到root下面后,就可以

createdb

默认创建名为root的数据库。然后psql命令就可以连接到数据库了。

相关内容