Ruby 让Postgres activerecord设置在本地和Heroku中工作
我正在自动创建数据库(在Sinatra应用程序中使用Rakefile) 我希望能够从我的Linux用户“pete”(例如pete@pete_laptop:/path$rake db:create)和Heroku 这取决于my config/database.rb中的设置:Ruby 让Postgres activerecord设置在本地和Heroku中工作,ruby,postgresql,ubuntu,activerecord,heroku,Ruby,Postgresql,Ubuntu,Activerecord,Heroku,我正在自动创建数据库(在Sinatra应用程序中使用Rakefile) 我希望能够从我的Linux用户“pete”(例如pete@pete_laptop:/path$rake db:create)和Heroku 这取决于my config/database.rb中的设置: ActiveRecord::Base.establish_connection( :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:port => db.port,
# pete@ubuntu_14.04_laptop--------
# :username => 'pete',
# :password => 'password',
# OR
# heroku -----------------
# :username => db.user,
# :password => db.password,
:database => DB_NAME,
:encoding => 'utf8'
)
如果我使用pete@ubuntu_laptop设置时,数据库在localhost中工作,但在Heroku中不工作,
如果使用heroku设置,则数据库在localhost中工作,但在heroku中不工作
如何设置此文件/我的ubuntu笔记本电脑,使应用程序在本地主机和Heroku上都能运行
干杯
Pete您可以使用环境变量,这些变量可以像Ruby中的
ENV[“PG_USER”]
那样访问。如果您想在yml文件中使用它,可以将其放入erb标记
中,并在将其传递到配置之前使用erb进行渲染
您可以在.bashrc
中设置环境变量,也可以使用dotenv gem之类的工具。在heroku上,您可以设置环境变量,如heroku config:set PG_USER=postgres
但是检查一下这对heroku来说是否真的是必要的。例如,在Rails中,heroku提供了数据库配置,因此无需对其进行配置 好的!在别人的帮助下,它开始工作了! 使用: 及 将此添加到我的shell rc(在我的示例中~/.zshrc) 现在,在我的本地环境中,用户名和密码从我启动应用程序的终端获取ENV['PG..]变量
注意:“导出”很重要-没有它,变量就不会被发送到应用程序的“ENV”谢谢jphager2!这正是我最终成功的原因!
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:port => db.port,
:username => ENV['PG_USER'] || db.user,
:password => ENV['PG_PASSWORD'] || db.password,
:database => DB_NAME,
:encoding => 'utf8'
)
export PG_USER='pete'
export PG_PASSWORD='password'