且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-24 09:34:03

从CLI:

  $ su  -  postgres 
$ psql template1
template1 =#CREATE USER tester with PASSWORD'test_password';
template1 =#将所有特权授予数据库test_database以测试;
template1 =#\q



PHP PHP(在localhost上测试, ):

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


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

I created the database and user in PostgreSQL.

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?

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):

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