如何在PostgreSQL中的数据库创建用户? [关闭](How to create user

2019-06-25 03:16发布

我有我的CentOS的服务器上安装的PostgreSQL 8.4和从外壳和访问PostgreSQL的外壳连接到root用户。

我创建了PostgreSQL的数据库和用户。

当试图从我的PHP脚本连接它表明我的身份验证失败。

如何创建一个新用户,如何授予特定DB的权限呢?

Answer 1:

从CLI:

$ su - postgres 
$ psql template1
template1=# CREATE USER tester WITH PASSWORD 'test_password';
template1=# GRANT ALL PRIVILEGES ON DATABASE "test_database" to tester;
template1=# \q

PHP(如在本地主机上测试,它按预期工作):

  $connString = 'port=5432 dbname=test_database user=tester password=test_password';
  $connHandler = pg_connect($connString);
  echo 'Connected to '.pg_dbname($connHandler);


Answer 2:

创建一个带有密码的用户:

http://www.postgresql.org/docs/current/static/sql-createuser.html

CREATE USER name [ [ WITH ] option [ ... ] ]

where option can be:

      SUPERUSER | NOSUPERUSER
    | CREATEDB | NOCREATEDB
    | CREATEROLE | NOCREATEROLE
    | CREATEUSER | NOCREATEUSER
    | INHERIT | NOINHERIT
    | LOGIN | NOLOGIN
    | REPLICATION | NOREPLICATION
    | CONNECTION LIMIT connlimit
    | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
    | VALID UNTIL 'timestamp'
    | IN ROLE role_name [, ...]
    | IN GROUP role_name [, ...]
    | ROLE role_name [, ...]
    | ADMIN role_name [, ...]
    | USER role_name [, ...]
    | SYSID uid

然后授予特定的数据库上的用户权限:

http://www.postgresql.org/docs/current/static/sql-grant.html

例如:

grant all privileges on database db_name to someuser;


文章来源: How to create user for a db in postgresql? [closed]