我刚刚安装了postgresql,在安装过程中我指定了密码x。 当我尝试做createdb并指定任何密码时,我得到消息:

createdb:无法连接数据库postgres: FATAL:用户密码验证失败

createuser也一样。

我该怎么开始呢? 我可以将自己作为用户添加到数据库吗?


当前回答

在MacOS中,我按照以下步骤使其工作。

首次安装时,请在安装完成后获取系统用户名。

$ cd ~
$ pwd
/Users/someuser
$ psql -d postgres -U someuser

现在您已经登录到系统,可以创建DB了。

postgres=# create database mydb;
CREATE DATABASE
postgres=# create user myuser with encrypted password 'pass123';
CREATE ROLE
postgres=# grant all privileges on database mydb to myuser;
GRANT

其他回答

其他的答案都不能让我完全满意。以下是Xubuntu 12.04.1 LTS上postgresql-9.1的工作原理。

Connect to the default database with user postgres: sudo -u postgres psql template1 Set the password for user postgres, then exit psql (Ctrl-D): ALTER USER postgres with encrypted password 'xxxxxxx'; Edit the pg_hba.conf file: sudo vim /etc/postgresql/9.1/main/pg_hba.conf and change "peer" to "md5" on the line concerning postgres: local      all     postgres     peer md5 To know what version of postgresql you are running, look for the version folder under /etc/postgresql. Also, you can use Nano or other editor instead of VIM. Restart the database : sudo /etc/init.d/postgresql restart (Here you can check if it worked with psql -U postgres). Create a user having the same name as you (to find it, you can type whoami): sudo createuser -U postgres -d -e -E -l -P -r -s <my_name> The options tell postgresql to create a user that can login, create databases, create new roles, is a superuser, and will have an encrypted password. The really important ones are -P -E, so that you're asked to type the password that will be encrypted, and -d so that you can do a createdb. Beware of passwords: it will first ask you twice the new password (for the new user), repeated, and then once the postgres password (the one specified on step 2). Again, edit the pg_hba.conf file (see step 3 above), and change "peer" to "md5" on the line concerning "all" other users: local      all     all     peer md5 Restart (like in step 4), and check that you can login without -U postgres: psql template1 Note that if you do a mere psql, it will fail since it will try to connect you to a default database having the same name as you (i.e. whoami). template1 is the admin database that is here from the start. Now createdb <dbname> should work.

Note: textdb is the database which you are going to explore with 'alex' user 

root@kalilinux:~# sudo su - postgres 
postgres=#  psql   
postgres=#  create database testdb;
postgres=#  create user alex with password 'alex';
postgres=# GRANT ALL PRIVILEGES ON DATABASE testdb TO alex;`enter code here`

您可能需要更新pg_hba.conf文件。该文件控制哪些用户可以从哪些IP地址登录。我认为postgres用户在默认情况下是被锁定的。

只要浏览到你的安装目录,并执行这个文件“pg_env.bat”,所以在bin文件夹和执行pgAdmin.exe。毫无疑问,这一定有用!

在MacOS中,我按照以下步骤使其工作。

首次安装时,请在安装完成后获取系统用户名。

$ cd ~
$ pwd
/Users/someuser
$ psql -d postgres -U someuser

现在您已经登录到系统,可以创建DB了。

postgres=# create database mydb;
CREATE DATABASE
postgres=# create user myuser with encrypted password 'pass123';
CREATE ROLE
postgres=# grant all privileges on database mydb to myuser;
GRANT