【问题标题】:Rails, datatabse creation issueRails,数据库创建问题
【发布时间】:2021-04-22 04:28:46
【问题描述】:

我正在尝试使用默认用户“postgres”创建数据库 但是在执行 rails db:create 时没有选择角色名称 postgres,它正在选择我的系统名称“mysystem”。

下面是我的database.yml文件代码

default: &default
 adapter: postgresql
  encoding: unicode
 # For details on connection pooling, see Rails configuration guide
 # https://guides.rubyonrails.org/configuring.html#database-pooling
 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

 development:
  <<: *default
  database: user_development
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>

 test:
  <<: *default
  database: user_test

 production:
  <<: *default
  host: <%= ENV['DATABASE_HOST_URL'] %>
  database: user_production
  username: <%= ENV['USER_DATABASE_USERNAME'] %>
  password: <%= ENV['USER_DATABASE_PASSWORD'] %>

执行rails db:create时出错

 rails aborted!
  ActiveRecord::ConnectionNotEstablished: FATAL:  role "mysystem" does not exist      

 Caused by:
  PG::ConnectionBad: FATAL:  role "mysystem" does not exist

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

    这里有一些有用的信息: https://guides.rubyonrails.org/v4.2.7/configuring.html#configuring-a-database 第 3.12 和 3.13 节

    我认为您的问题可能是主机:设置。这应该要么 是 /tmp 用于 unix 域套接字,或者是 IP 地址/主机名,如果使用的话。

    我在测试中使用的设置: postgresql 12.3 debian unix。

    Using this pg_hba.conf:
    local   all             all                                     ident
    host    all             all             127.0.0.1/32            md5
    host    all             all             ::1/128                 md5
    host    all             all             192.168.50.0/24         md5
    

    您可能需要一个 hostssl 条目,用于在机器之间发送数据。

    # using the same "user" database name you have. 
    rails new user -d postgresql
    
    #setting the password for testing.
    psql
    alter role user1 password 'testpass';
    
    cd user/config
    emacs database.yml
    
    #example using ident authentication - being logged in as the same unix user.
    production:
      <<: *default
      host: /tmp
      database: user_production
      username: user1
      password: nil
    
    # using a host ip address
    production:
      <<: *default
      host: 192.168.50.6
      database: user_production
      username: user1
      password: testpass
    
    # using a host ip address with env vars
    #export USER_DATABASE_USERNAME="user1"
    #export USER_DATABASE_PASSWORD="testpass"
    production:
      <<: *default
      host: 192.168.50.6
      database: user_production
      username: <%= ENV['USER_DATABASE_USERNAME'] %>
      password: <%= ENV['USER_DATABASE_PASSWORD'] %>
    
    # hardcoding everything in the url for illustration:
    production:
      <<: *default
      url: postgres://user1:testpass@192.168.50.6/user_production
    
    # best practice according to doc. DATABASE_URL has precedence over database.yaml when set
    # we are just indicating we know that here.
    # export DATABASE_URL="postgres://user1:testpass@192.168.50.6/user_production"
    production:
      <<: *default
      url: <%= ENV['DATABASE_URL'] %>
    
    user1@debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:create
    Created database 'user_production'
    
    user1@debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:drop
    Dropped database 'user_production'
    
    #Seeing what is being used:
    RAILS_ENV=production bin/rails runner 'p ActiveRecord::Base.configurations'
    
    #<ActiveRecord::DatabaseConfigurations:0x0000564e40093fa0 @configurations=[#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e40092f60 @env_name="default", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400928f8 @env_name="development", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_development"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400921a0 @env_name="test", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_test"}>, #<ActiveRecord::DatabaseConfigurations::UrlConfig:0x0000564e40091b60 @env_name="production", @name="primary", @configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :username=>"user1", :password=>"testpass", :database=>"user_production", :host=>"192.168.50.6"}, @url="postgres://user1:testpass@192.168.50.6/user_production">]>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多