[英]How do I set up a database for a Sinatra app in Heroku?
I made a Sinatra application that runs with a SQLite3 database. 我制作了一个运行SQLite3数据库的Sinatra应用程序。 Everything runs smoothly on my local machine.
一切都在我的本地计算机上顺利运行。
When I am deployed it to Heroku I had to change a few things, namely a Gemfile and a Procfile, while also changing my database code to this: 将其部署到Heroku时,我必须更改一些东西,即Gemfile和Procfile,同时还要将数据库代码更改为:
#Within a class
#Before
@DB = Sequel.connect('sqlite://database.db')
#After
@DB = Sequel.connect(ENV['DATABASE_URL'] || 'postgres://localhost/db')
I changeed the database to Postgres, rather than SQLite3 because SQLite3 isn't supported with Heroku. 我将数据库更改为Postgres,而不是SQLite3,因为Heroku不支持SQLite3。 (This may be where my problem stems from.)
(这可能是我的问题所在。)
Here is my Gemfile: 这是我的Gemfile:
source "https://rubygems.org"
ruby "1.9.3"
gem "sinatra"
gem "sequel"
gem "pg"
gem "postgres"
gem "json"
And my Procfile: 而我的Procfile:
web: bundle exec ruby main.rb -p $PORT
I made a git repository, and ran heroku create
. 我创建了一个git存储库,并运行
heroku create
。 I added all the files, committed and pushed them using git and I got my Heroku URL and everything is fine. 我添加了所有文件,使用git提交并推送了它们,我得到了Heroku URL,一切都很好。 I ran
heroku config
to check my URLs and got this result: 我运行
heroku config
来检查我的URL并得到以下结果:
DATABASE_URL:postgres://uhyodailuzazve:hUSIuD_4MOYWmoiSGNZCPrQNyg@ec2-54-204-40- 140.compute-1.amazonaws.com:5432/d1qkn1ivl7p013HEROKU_POSTGRESQL_BLACK_URL:postgres://uhyodailuzazve:hUSIuD_4MOYWmoiSGNZCPrQNyg@ec2-54-204-40-140.compute-1.amazonaws.com:5432/d1qkn1ivl7p013
(They are the same). (他们是一样的)。 Also my stack is "cedar"
我的筹码也是“雪松”
=== enigmatic-taiga-5020 Available Stacks
bamboo-mri-1.9.2
bamboo-ree-1.8.7
* cedar
How can I have a database configured with Sinatra, going from Sequel to Postgres? 如何从Sequel到Postgres使用Sinatra配置数据库? I've looked at tutorials and they have gotten this far.
我看过教程,到现在为止。 Has anyone done this before?
有人做过吗?
@DB = Sequel.connect(ENV['DATABASE_URL'])
应该可以正常工作,尽管通常您会将数据库分配给常量,而不是实例变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.