我的应用程序使用 Heroku,它需要 PostgreSQL,但您仍然可以使用 SQLite3 进行开发。由于 Heroku 强烈建议不要使用 2 个不同的数据库,因此我决定改用 PostgreSQL 进行开发。我安装了 gem pg
,还去了官方 PostgreSQL 站点获取 Windows 安装程序,还更改了我的 database.yml
。在安装过程中,它需要 PostgreSQL 的密码,所以我做了一个。
我必须将 pg_hba.conf
文件从使用 md5
更改为 trust
才能通过:fe_sendauth: no password supplied
尝试创建数据库时。
# 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
尽管摆脱了它,我现在明白了:
$ 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
和 text.sqlite3
仍然存在,这可能是问题所在吗?必须做什么?
这是我的完整要点:https://gist.github.com/1522188
最佳答案
将用户名添加到您的 database.yml
,也可以使用您的应用程序名称(或名称的某些变体)作为用户名,我将使用 app_name
作为占位符:
development:
adapter: postgresql
encoding: utf8
database: app_development
pool: 5
username: app_name
password:
然后使用 psql.exe
在 PostgreSQL 中创建用户(又名“角色”):
$ psql -d postgres
postgres=# create role app_name login createdb;
postgres=# \q
第一行在您的终端中,接下来的两行在 psql
中。然后执行您的 rake db:create
。
User
用户可能是默认用户,但 user
is already taken用于 PostgreSQL 中的其他目的,因此如果您想将 User
用作用户名,则必须引用它以保留大小写:
postgres=# create role "User" login createdb;
无论如何,您最好为每个应用程序创建一个用户。
您还需要对 database.yml
中的 test
条目执行类似的操作。
关于ruby-on-rails - 使用 PostgreSQL 时角色不存在且无法创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8639424/