使用PostgreSQL时角色不存在无法创建数据库 [英] Role does not exist and unable to create database when using PostgreSQL

查看:88
本文介绍了使用PostgreSQL时角色不存在无法创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用 Heroku,它需要 PostgreSQL,但您仍然可以使用 SQLite3 进行开发.由于 Heroku 强烈建议不要使用 2 个不同的数据库,因此我决定更改为 PostgreSQL 进行开发.我安装了 gem pg 并前往官方 PostgreSQL 站点获取 Windows 安装程序,并更改了我的 database.yml.在安装过程中它需要 PostgreSQL 的密码,所以我做了一个.我必须将 pg_hba.conf 文件从使用 md5 更改为 trust 才能通过:fe_sendauth:未提供密码code> 尝试创建数据库时.

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.sqlite3text.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:

然后使用 psql.exe 在 PostgreSQL 中创建用户(又名角色"):

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 用户可能是默认用户,但 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆