rake db:使用PostgreSQL创建---- fe_sendauth:没有提供密码

时间:2015-03-29 11:50:04

标签: 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)像我这样的新手可以在哪里阅读更多有关此内容的信息,以便更好地理解(目前没有绝对技术性的)正在发生的事情以及developmenttestproduction配置和环境? Rails文档?

1 个答案:

答案 0 :(得分:4)

我不喜欢这个。

sudo -u postgres createuser tjjjwxzq -s

没有理由让该用户成为整个群集的超级用户(-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

您已设置pg_hba.conf以信任套接字上的连接;取消注释这一行会使Rails尝试使用tcp而不是套接字进行连接。

现在正常工作。

$ bin/rake db:create