ruby-on-rails - rake 数据库 :create using PostgreSQL ---- fe_sendauth: no password supplied

标签 ruby-on-rails postgresql

这一切都是新手,所以请耐心等待。基本上我是想关注 this在我的 Linux Lite 机器上安装 Rails。安装 PostgreSQL 后,我继续按照教程说明创建数据库角色,如下所示

sudo -u postgres createuser tjjjwxzq -s

但我没有跟进为这个角色创建密码,因为教程让它看起来像是一个可选的东西。我在我的 database.yml 文件中指定了开发环境用户名:

development:
  <<: *default
  database: myapp_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  username: tjjjwxzq

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

(编辑:我取消注释 host:localhost)

我还更改了 pg_hba.conf 文件以使用我创建的角色进行身份验证:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# settings for tjjjwxzq user
local   all             tjjjwxzq                                     trust

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

如您所见,我将tjjjwxzq 用户的身份验证方法设置为trust,因为我没有创建密码而且我不想创建密码供应一个。但是当我运行时,在我的 Rails 项目目录中

rake db:create

我得到:

fe_sendauth: no password supplied
...
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"myapp_test"}

我对 PostgreSQL 和 Rails 完全陌生,现在我已经在谷歌上搜索了几个小时的解决方案。更改 pg_hba.conf 文件似乎很有希望,但它对我不起作用。我怀疑我可以通过为我创建的 tjjjwxzq 角色指定密码来通过此操作,但我想了解为什么我的设置现在不起作用。非常感谢任何帮助。

编辑: 好吧,我意识到我犯了一个愚蠢的错误。 rake db:create 确实可以创建 myapp_development(指定的开发数据库)我已经检查了日志文件,最新的日志是:

2015-04-01 07:04:28 SGT [2342-1] tjjjwxzq@postgres ERROR:  database "myapp_development" already exists
2015-04-01 07:04:28 SGT [2342-2] tjjjwxzq@postgres STATEMENT:  CREATE DATABASE "myapp_development" ENCODING = 'unicode'

现在我意识到在 rake db:create 的输出中,最后一行显示

Couldn't create database for {"adapter"=>"postgresql", encoding"=>"unicode", "pool"=>5, "database"=>"myapp_test"}

所以问题实际上出在我的测试数据库上,查看我的 database.yml 文件,我看到:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: myapp_test

所以没有username字段。到目前为止,感谢您的帮助,但现在我的问题是:1) myapp_test 没有成功创建是否可以,或者我应该将其用户设置为 tjjjwxzq 作为出色地? 2) 像我这样的新手可以在哪里阅读更多相关内容,以更好地理解(但目前没有压倒性的技术)正在发生的事情以及 development 的需求,测试生产 配置和环境? rails 文档?

最佳答案

我不喜欢这个。

sudo -u postgres createuser tjjjwxzq -s

没有理由让该用户成为整个集群的 super 用户 (-s)。只允许该用户创建数据库。

sudo -u postgres createuser tjjjwxzq --createdb

更改您的 config/database.yml 以使用套接字。注释掉 host: localhost 行。 (我假设您没有在 Windows 下运行。)

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  # host: localhost

You had set up your pg_hba.conf to trust connections on sockets; uncommenting this line makes Rails try to connect using tcp instead of sockets.

This should work now.

$ bin/rake db:create

关于ruby-on-rails - rake 数据库 :create using PostgreSQL ---- fe_sendauth: no password supplied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29328733/

相关文章:

ruby-on-rails - rails : Switch User Gem and issues with switching back to original user

javascript - 单页应用程序和 CSRF token

r - 使用 RPostgresql 打印查询时间

Postgresql 使用时区 UTC 创建表字段名时间戳

ruby-on-rails - Rspec & Rails : how to test controller for 404 error? 它给出了 "no route matches"错误而不是 404

ruby-on-rails - Rails 2.3.14:如何序列化 ActionController::Request 对象?

ruby-on-rails - 如何在 Ruby Controller 中加载凭据而不将它们存储在文件中?

sql - PostgreSQL 解释 : why does child have larger cost than parent?

SQL order and limit then order the limited 结果

node.js - Sequelize -自动 : Changing Postgres Table Schemas