我刚刚安装了postgresql,在安装过程中我指定了密码x。 当我尝试做createdb并指定任何密码时,我得到消息:
createdb:无法连接数据库postgres: FATAL:用户密码验证失败
createuser也一样。
我该怎么开始呢? 我可以将自己作为用户添加到数据库吗?
我刚刚安装了postgresql,在安装过程中我指定了密码x。 当我尝试做createdb并指定任何密码时,我得到消息:
createdb:无法连接数据库postgres: FATAL:用户密码验证失败
createuser也一样。
我该怎么开始呢? 我可以将自己作为用户添加到数据库吗?
当前回答
有两种方法可以使用。两者都需要创建用户和数据库。
Using createuser and createdb, $ sudo -u postgres createuser --superuser $USER $ createdb mydatabase $ psql -d mydatabase Using the SQL administration commands, and connecting with a password over TCP $ sudo -u postgres psql postgres And, then in the psql shell CREATE ROLE myuser LOGIN PASSWORD 'mypass'; CREATE DATABASE mydatabase WITH OWNER = myuser; Then you can login, $ psql -h localhost -d mydatabase -U myuser -p <port> If you don't know the port, you can always get it by running the following, as the postgres user, SHOW port; Or, $ grep "port =" /etc/postgresql/*/main/postgresql.conf
旁注:postgres用户
我建议不要修改postgres用户。
It's normally locked from the OS. No one is supposed to "log in" to the operating system as postgres. You're supposed to have root to get to authenticate as postgres. It's normally not password protected and delegates to the host operating system. This is a good thing. This normally means in order to log in as postgres which is the PostgreSQL equivalent of SQL Server's SA, you have to have write-access to the underlying data files. And, that means that you could normally wreck havoc anyway. By keeping this disabled, you remove the risk of a brute force attack through a named super-user. Concealing and obscuring the name of the superuser has advantages.
其他回答
编辑:警告:请阅读埃文·卡罗尔发布的答案。看来这种方法不安全,不推荐使用。
在标准的Ubuntu 14.04 64位安装中,这对我来说是有效的。
我按照http://suite.opengeo.org/4.1/dataadmin/pgGettingStarted/firstconnect.html上的说明做了一些小修改
安装postgreSQL(如果你的机器上还没有安装):
Sudo apt-get安装postgresql
使用postgres用户运行psql
sudo –u postgres psql postgres
设置postgres用户的新密码:
\password postgres
退出 psql
\q
编辑/etc/postgresql/9.3/main/pg_hba.conf,修改如下:
使用Unix域套接字登录数据库 本地所有postgres peer
To:
使用Unix域套接字登录数据库 本地所有postgres md5
重启postgreSQL:
Sudo服务postgresql重启
创建一个新数据库
Sudo -u postgres createdb mytestdb
再次使用postgres用户运行psql:
psql –U postgres –W
列出现有的数据库(你的新数据库现在应该在那里):
\l
您可能需要更新pg_hba.conf文件。该文件控制哪些用户可以从哪些IP地址登录。我认为postgres用户在默认情况下是被锁定的。
在Linux下,PostgresQL通常配置为允许根用户以postgres超级用户postgres的身份从shell(控制台或ssh)登录。
$ psql -U postgres
然后像往常一样创建一个新数据库:
CREATE ROLE myuser LOGIN password 'secret';
CREATE DATABASE mydatabase ENCODING 'UTF8' OWNER myuser;
这应该在不接触pg_hba.conf的情况下工作。如果你想通过网络使用一些GUI工具来做到这一点,那么你就需要修改pg_hba.conf。
在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
只要浏览到你的安装目录,并执行这个文件“pg_env.bat”,所以在bin文件夹和执行pgAdmin.exe。毫无疑问,这一定有用!