php 如何在 postgresql 中为数据库创建用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10861260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:15:02  来源:igfitidea点击:

How to create user for a db in postgresql?

phpdatabasepostgresqlauthenticationconnection

提问by Vidul

I have installed PostgreSQL 8.4 on my CentOS server and connected to root user from shell and accessing the PostgreSQL shell.

我已经在我的 CentOS 服务器上安装了 PostgreSQL 8.4 并从 shell 连接到 root 用户并访问 PostgreSQL shell。

I created the database and user in PostgreSQL.

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

While trying to connect from my PHP script it shows me authentication failed.

尝试从我的 PHP 脚本连接时,它显示我的身份验证失败。

How do I create a new user and how to grant permissions to them for a particular DB?

如何创建新用户以及如何向他们授予特定数据库的权限?

回答by Vidul

From 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 (as tested on localhost, it works as expected):

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

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

回答by Denys Séguret

Create the user with a password :

使用密码创建用户:

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

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

Then grant the user rights on a specific database :

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

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

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

Example :

例子 :

grant all privileges on database db_name to someuser;