我刚刚安装了postgresql,在安装过程中我指定了密码x。 当我尝试做createdb并指定任何密码时,我得到消息:
createdb:无法连接数据库postgres: FATAL:用户密码验证失败
createuser也一样。
我该怎么开始呢? 我可以将自己作为用户添加到数据库吗?
我刚刚安装了postgresql,在安装过程中我指定了密码x。 当我尝试做createdb并指定任何密码时,我得到消息:
createdb:无法连接数据库postgres: FATAL:用户密码验证失败
createuser也一样。
我该怎么开始呢? 我可以将自己作为用户添加到数据库吗?
当前回答
这是我的解决方案:
su root
su postgres
psql
其他回答
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`
在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
这是我的解决方案:
su root
su postgres
psql
如果你像我一样运行macOS,你可能没有postgres用户。
当试图运行sudo -u postgres psql时,我得到了错误sudo: unknown user: postgres
幸运的是,postgres提供了可执行文件。
createuser -D /var/postgres/var-10-local --superuser --username=nick
createdb --owner=nick
然后我就可以毫无问题地访问psql了。
psql
psql (10.2)
Type "help" for help.
nick=#
如果您要从头创建一个新的postgres实例,下面是我所采取的步骤。我使用了一个非默认端口,因此可以运行两个实例。
mkdir /var/postgres/var-10-local
pg_ctl init -D /var/postgres/var-10-local
然后我用我喜欢的端口5433编辑/var/ postgresql.conf。
/Applications/Postgres.app/Contents/Versions/10/bin/postgres -D /Users/nick/Library/Application\ Support/Postgres/var-10-local -p 5433
createuser -D /var/postgres/var-10-local --superuser --username=nick --port=5433
createdb --owner=nick --port=5433
完成了!
您可能需要更新pg_hba.conf文件。该文件控制哪些用户可以从哪些IP地址登录。我认为postgres用户在默认情况下是被锁定的。