在本地使用远程Postgres连接开发Rails应用?

8

有没有办法配置database.yml文件以远程连接Heroku的Postgres数据库?

我不太明白Heroku、Rails和PG gem是如何协同工作的。

看起来在部署过程中,Heroku会重新编写database.yml文件——是否有可能查看这个更新后的.yml文件的内容并在本地使用它呢?

3个回答

29

以下是从本地开发访问Heroku数据库的步骤:

  1. 登录您的Heroku帐户。

  2. 导航至此URL https://postgres.heroku.com/databases/,或者您可以在终端中运行此命令获取Heroku数据库的URL: heroku pg:credentials:url

  3. 选择您的应用程序数据库。

  4. 您将能够看到Heroku PG凭证:

Host         xxxxxxxxx.77777.amazonaws.com
Database     42feddfddeee 
Username         44444444444
Port         xxxx
Password     777sfsadferwefsdferwefsdf
  • 收集以上信息并将其放入您的database.yml文件中的development环境:

  • adapter: postgresql  
    host: < host >                            # HOST 
    port: < port >                            # Port  
    database: < database name >               # Database Name  
    username: < user_name >                   # User Name  
    password: '< password >'                  # Password 
    

    请重启您的应用程序,

    祝你好运..!!


    3
    谢谢。对于任何阅读此内容的人:请注意在database.yml中使用“username:”而不是“user:”,否则它将尝试将您连接到系统名称。 - dsp_099

    2

    虽然我不是postgres的用户,但这应该有效。你可以修改你的database.yml文件以包含hostport

    production:
      adapter: postgresql
      encoding: unicode
      database: di_production
      pool: 5
      username: user
      password:
      host: heroku.host.name
      port: <postgres listen port>
    

    当然,你应该允许服务器端的连接,包括防火墙和数据库层面。

    查看 #{Rails.root}/config/database.yml 内容的简单方法是编写代码将此yml文件加载到对象中,然后在UI上打印出来。

    DB_CONFIG = YAML.load(File.read("#{Rails.root}/config/database.yml", __FILE__))
    puts DB_CONFIG["production"].inspect  # or what ever method you want to print it
    

    1
    我是一个新手,可能误解了你的问题。我使用PostgreSQL数据库开发了一个ROR应用程序。然后上传到Heroku。我运行了DB/Migrate/schema.rb来设置远程PostgreSQL数据库。
    从记忆中,Heroku运行rake db:init(但我可能错了)。每当我在开发中更新数据库以在Heroku中获得更新时,我必须推广代码并运行rake db:migrate
    从我的config/database.yml
    development:
      adapter: postgresql
      encoding: unicode
      database: di_development
      pool: 5
      username: davidlee
      password:
    test:
      adapter: postgresql
      encoding: unicode
      database: di_test
      pool: 5
      username: davidlee
      password:
    production:
      adapter: postgresql
      encoding: unicode
      database: di_production
      pool: 5
      username: user
      password:
    
    • 它能够正常工作。我记不得做过其他任何事情。

    Pierre


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接