使用PostgreSQL时角色不存在并且无法创建数据库 [英] Role does not exist and unable to create database when using PostgreSQL
问题描述
我正在为我的应用程序使用Heroku,它需要PostgreSQL,但是您仍然可以使用SQLite3进行开发。由于Heroku强烈建议不要使用2个不同的数据库,因此我决定改用PostgreSQL进行开发。我安装了 gem pg
,还去了PostgreSQL官方站点以获取Windows安装程序,还更改了我的 database.yml
。在安装过程中,它需要PostgreSQL的密码,所以我做了一个。
我不得不将 pg_hba.conf
文件从使用 md5
更改为 trust
才能通过: fe_sendauth:尝试创建数据库时未提供密码
。
I am using Heroku for my application and it requires PostgreSQL but you can still use SQLite3 for development. Since Heroku strongly advised against having 2 different databases I decided to change to PostgreSQL for development. I installed the gem pg
and also went to the official PostgreSQL site to get the Windows installer and also changed my database.yml
. During installation it requires a password for PostgreSQL so I made one.
I had to change the pg_hba.conf
file from using md5
to trust
in order get past: fe_sendauth: no password supplied
when trying to create the database.
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust # was md5
# IPv6 local connections:
host all all ::1/128 trust # was md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 trust
尽管摆脱了这些,我现在得到了:
After getting rid of that though, I now get this:
$ rake db:create
(in C:/app)
FATAL: role "User" does not exist
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8",
"database"=>"app_test", "pool"=>5, "username"=>nil, "password"=>nil}
我仍然有我的 development.sqlite3
和文本。 sqlite3
存在,这可能是问题吗?必须做什么?
I do still have my development.sqlite3
and text.sqlite3
present, could that be the issue? What must be done?
这是我的全部要旨: https ://gist.github.com/1522188
推荐答案
将用户名添加到您的 database.yml
,以及将应用程序名称(或名称的某些变体)用作用户名,我将使用 app_name
作为占位符:
Add a username to your database.yml
, might as well use your application's name (or some variant of the name) as the username, I'll use app_name
as a placeholder:
development:
adapter: postgresql
encoding: utf8
database: app_development
pool: 5
username: app_name
password:
然后创建PostgreSQL中使用 psql.exe
的用户(又称角色):
Then create the user (AKA "role") inside PostgreSQL using psql.exe
:
$ psql -d postgres
postgres=# create role app_name login createdb;
postgres=# \q
第一行在您的终端中,后两行在 psql
内部。然后执行 rake db:create
。
The first line is in your terminal, the next two are inside psql
. Then do your rake db:create
.
用户
用户可能是默认用户,但 user
是默认用户已在PostgreSQL中用于其他用途,因此如果您想使用 User
作为用户名,则必须引用它以保留大小写:
The User
user is possibly a default but user
is already taken for other purposes in PostgreSQL so you'd have to quote it to preserve the case if you wanted to use User
as a username:
postgres=# create role "User" login createdb;
最好还是为每个应用程序创建一个用户。
You're better off creating one user per-application anyway.
您将要为 database.yml
中的 test
条目做类似的事情好吧。
You'll want to do similar things for your test
entry in database.yml
as well.
这篇关于使用PostgreSQL时角色不存在并且无法创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!