如何在 postgresql 中为数据库创建用户? [英] How to create user for a db in postgresql?

查看:19
本文介绍了如何在 postgresql 中为数据库创建用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I created the database and user in PostgreSQL.

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

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

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

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

推荐答案

来自 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(在本地主机上测试,它按预期工作):

PHP (as tested on localhost, it works as expected):

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

这篇关于如何在 postgresql 中为数据库创建用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆