Here's the story, my heroku site was originally using the 5mb shared postgres db with no problems. I had a valid SHARED_DATABASE_URL and no DATABASE_URL
Then I added a config var by doing
heroku config:add DATABASE_URL=non_existing_database_just_for_fun
Just to see if it would switch from the shared db to this new one I just set. It did just that, and promptly crashed my app as expected since no valid database was found.
Then I did heroku config:remove DATABASE_URL hoping to get my site back to normal. But now it keeps crashing and never succeeds in starting up. If I do a heroku config I see that I still have a valid SHARED_DATABASE_URL and no DATABASE_URL but the site still wont work.
I did get it working by setting up DATABASE_URL to match SHARED_DATABASE_URL but I'd like to get it back to how things were originally, the site working without needing DATABASE_URL. Any ideas no how I can get things back to the way they were short of having to reinstall my site?
DATABASE_URL is the key that Heroku expect you to use for your database connectivity. They will not touch this value unless you ask them to (aside from initial setup).
The SHARED_DATABASE_URL is the URL of the shared database that they have provided to you.
By default, Heroku set your DATABASE_URL to match your SHARED_DATABASE_URL.
I'm not entirely sure why would would want your application to not have a DATABASE_URL, as that's what's used. If you look at the bottom of this, you can see what they do with your config/database.yml and how it affects your application.
Related
I'm working on a Rails app with a few collaborators and we decided to begin using separate database.yml files for some time until we can a configuration that works for all of us.
After adding database.yml to the .gitignore file and pushing a version without it, I realized that this would likely prevent the Heroku app from running.
My confusion is that the deployment was successful and the database.yml file was not needed. Why is this? Is our old database.yml file cached?
This is actually the expected behavior. For more details see: https://devcenter.heroku.com/articles/rails-database-connection-behavior
Which boils down to (for Rails 4.1+):
While the default connection information will be pulled from
DATABASE_URL, any additional configuration options in your
config/database.yml will be merged in.
Heroku will always use DATABASE_URL and merge the rest from database.yml to the config contained in that url.
Ah yes the old db config developer war.
Heroku actually uses the solution to this issue - Rails merges the database configuration from database.yml with a hash created from parsing ENV["DATABASE_URL"]. The ENV var takes precedence over the file based configuration.
When you first push a Rails app, Heroku automatically attaches a Postgres addon and sets ENV["DATABASE_URL"] and presto your app magically connects to the database.
Even if you add complete nonsense settings like setting the database name in database.yml the ENV var still wins.
How can this solve our developer war?
Do the opposite of what you are currently doing. Strip everything except the bare minimum required to run the application out of database.yml and check it back into version control.
Developers can use direnv or one of the many tools available to set ENV[DATABASE_URL] to customize the settings while database.yml should be left untouched unless you actually need to tweak the db.
I have an app up and running on heroku connected to it's own database, however i have a database in my own server which is free and I want to connect my app to it, I changed my Gemfile accordingly but it seems to just ignore it.
I tried
heroku config:add DATABASE_URL=url
but it said he couldn't replace the existing DATABASE_URL...
You can either do this via the command line by first removing and then adding the variable:
heroku config:remove DATABASE_URL
heroku config:add DATABASE_URL=http://someurl.com
or you can do this via the heroku dashboard.
Select your app
Go to Settings
Reveal config variables
Change your DATABASE_URL
This one took me a while to figure out. It's pretty tricky and seems like a bug in Heroku, so I will post my own answer in case it helps someone.
Scenario:
Rails application using postgresql adapter works fine on localhost.
All migrations are complete.
Deploying to Heroku via "git push heroku master" successfully updates Heroku instance.
Heroku PGBackups addon is installed and works fine.
pg_dump was used to generate a local db dump, and the file uploaded to an accessible internet location.
Problem:
After running
heroku pgbackups:restore DATABASE_URL 'http://mywebsite.com/pgbackup.dump'
I get the "something went wrong" message. Strange, considering all that happened was a database load, a database whose data works fine on the local machine. No migrations were performed, everything has been committed previously, no code updates... Only data in the database was changed, so why does the app no longer work if just data in the database changed?
Checking the heroku logs shows that it can't find tables that are clearly there. Lines like:
ActionView::Template::Error (PG::UndefinedTable: ERROR: relation "users" does not exist
Verifying with heroku pg:psql just makes the situation even stranger, because doing select * from users brings back the real results. Even performing a heroku restart at this stage won't get rid of the problem. I also tried combinations of that with heroku pg:reset and heroku run rake db:migrate.
Why does updating data in the database shut down the app?
Solution:
Run the pgbackups:restore as originally intended (and get error).
Run heroku restart.
Run heroku pg:psql (profit).
For some reason, it seems necessary to restart and then pg:psql before the database works after a pg:restore.
Even though pg:psql will gladly show that the tables are indeed there (contrary to the log messages), the app won't work until the dyno is restarted (and then it STILL won't work), not until you pg:psql.
As far as I know, pg:psql only establishes a connection to the remote database, and should not have any consequence on the state of the database server (though it seems it does).
Hey so I am following the One Month Rails guide to learning Ruby and I have hit a wall on one of the lessons. I have just finished uploading an image with Paperclip, and as I finished my work on my localhost and checked it on Heroku, something went wrong. The pin/image appears to have been pushed to my Heroku account, the only problem is that the username and password that works for my localhost:3000 won't work for my Heroku account. The same password should work for both, but for some reason something is wrong. I wish I could give you the action that is going on in my terminal, but the ruby rails is the only thing that has a continuous status flow. The problem may have been when I switched my password after not using my account on localhost for a few weeks, but i thought that once i "git pushed" that to heroku master, it would've synced. I have tried heroku run rake db:setup which didn't seem to do too much as well as wrestled by way through "Importing a Heroku Postgres Database with PG Backups", but I had some trouble working through that. Any ideas? Thanks for the help.
Your 'database.yml' should not be sent to Heroku, they take care of that, creating a new database.yml config file with the proper DB access details.
Try logging into your Heroku instance and deleting the file.
Edit: nevermind, assumed you were not able to connect to the DB, not to login into the website.
So if I'm understanding you correctly, you've deployed your application to Heroku and the login (to your application) that was working locally doesn't work on Heroku.
Deploying your application doesn't deploy data. Assuming you've run heroku run rake db:migrate then your database schemas will at least match.
At this point, you've got a couple of options.
Use a seeds.rb file which you can load with heroku run db:seed to setup some 'seed' data so that you can login.
Push your local database to Heroku - either via heroku db:push or using heroku pg:transfer provided by https://github.com/ddollar/heroku-pg-transfer
Use heroku run console to create your user account via the command line
User.create(email: 'someemail.com', password: 'somepassword', password_confirmation: 'somepassword')
I'd be inclined to go with the later option.
How did the user get in their in the first place? Perhaps going back to that step in the tutorial - just remember, if you are using rails console locally to use heroku run console on Heroku.
I've made 2 apps, App A and App B. App A's sole purpose is to allow users to sign up and App B's purpose is to take select users from App A email them. Since App A & B were created independently & are hosted in 2 separate Heroku instances, how can App B access the users database in App A? Is there a way to push certain relevant rows from App A to App B?
There is currently no way of sharing databases between Heroku apps.
You might be able to use the Amazon RDS add-on to run a dedicated MySQL instance.
The alternative is going to be creating an API and pushing data between the apps. You can run a background process to push the data in and out.
You can connect several Heroku instances on the same shared PostgreSQL database, given by the shared-database:5mb free add-on on Heroku.
On the instance on which you have the database, type:
$ heroku config
This will show a lot of settings, in which you'll see something like this:
DATABASE_URL => postgres://fbksgiuqlv:BuIvJDfS_eOBDJDZCc9SP#ec2-104-20-247-168.compute-1.amazonaws.com/fbksgiuqlv
This is the database connection string that your instances will be using.
Then on the other instances, overwrite the DATABASE_URL config variable by typing:
$ heroku config:add DATABASE_URL=your_new_connection_string
So tinkered a little around and did as below and it worked
prompt$ heroku console
Ruby console for your-app.heroku.com
>> dbconfig = YAML.load(File.read('config/database.yml'))
=> {"production"=>{"encoding"=>"unicode", "port"=>5432, "adapter"=>"postgresql", "username"=>"xxxxxxxxxxxxxx", "database"=>"xxxxxxxxxxxxx", "host"=>"kklasfdsfjsfdsfsd.fsdf.dsfdsf", "password"=>"xxxxxxxxxxxxxxxxxx"}}
puts dbconfig.to_yaml
---
production:
encoding: unicode
port: 5432
adapter: postgresql
username: xxxxxxxxxxx
database: xxxxxxxxxxxxxxx
host: ec2-50-2323kskdsakd231.amazonaws.com
password: xxxxxxxxxxxxxx
Then copy and paste the yaml to a connection for the other DB
and it works!!! For me!!!
I was looking into this as well and I noticed that it seems like it is now possible to actually do this.
See: http://newsletterer.heroku.com/2011/07 ("did you know" section at the bottom)
Basically you set up one app, retrieve the app database url, and add that url to the config of the other app, like so:
$ heroku config | grep DATABASE_URL --app sushi
DATABASE_URL => postgres://lswlmfdsfos:5FSLVUSLLT123#ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf
Then, set the DATABASE_URL for new apps to this value:
$ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:5FSLVUSLLT123#ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf --app sushi-analytics
Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf
Restarting app... done, v74.
That's it — now both apps will share one database.
I haven't tried this yet, but I am about to give it a shot, as I too was thinking about splitting an existing app into two apps. Let's hope this actually works!
Heroku actually overwrites the database.yml file that you have checked in, you can verify this by using the "heroku shell" command and typing cat config/databse.yml
They make up a bunch of random stuff that is used per application. I don't believe its possible to stop them from doing that.
You might be able to do this if you use schemas with PostgreSQL.