I have a ruby on rails app deployed on heroku with a database and a follower database.
I typically do "heroku run rails console" to look around the data, and I'm wondering if there's a way that I can do it on the follower database so I don't accidentally write/delete things. I know this should be simple, but I couldn't seem to find documentation on it.
Thanks!
You can do this from your local console. Just fire it up:
rails console
Then you need the connection information for your follower database on heroku. You can retrieve this from https://postgres.heroku.com/databases/. Just click the relevant database and assign a hash with the following info:
follower_db = {:adapter => "postgresql",
:host => "myhost",
:username => "myuser",
:password => "mypass",
:database => "somedatabase"}
Now connect to this remote database from the console:
ActiveRecord::Base.establish_connection(follower_db)
Now any query you enter in the console will be run on this DB.
This will use the follower database credentials from the FOLLOWER_DATABASE_URL environment variable in your Heroku app to start up a Rails console that connects directly to the follower instead of the promoted database. Normally Rails is set up to read from DATABASE_URL by default, and this just sets it to a different value for the duration of your console session.
heroku run 'DATABASE_URL=$FOLLOWER_DATABASE_URL rails console'
If you're worried about accidentally deleting stuff, run console with the sandbox flag
heroku run console --sandbox --app app-name
Your changes will be rolled back when your session ends.
The console certainly has its place, but if I want to check what data is in a database then I'd prefer to look directly at it without ActiveRecord etc being in the way. I've found many instances where using the console requires that you check the generated SQL anyway to ensure that there are no scopes (for example) slipping in.
I'd suggest that you try to get into the habit of using a SQL interface (PgAdminIII perhaps) instead of the console -- an Heroku dataclip will even work if you don't want to trouble about connecting from the client (though the psql connection string is given to you from the database page).
A database follower is a read-only copy of the master database that stays up-to-date with the master database data. As writes and other data modifications are committed in the master database the changes are streamed, in real-time, to the follower databases.
Source - https://devcenter.heroku.com/articles/heroku-postgres-follower-databases
Related
I've added heroku postgres addon.
The DATABASE_URL is something like postgres://xxxxxxxxx:xxxxxxxx#xxxxxxxxx.compute-1.amazonaws.com:5432/ddo2ahvosfggq
I want the database name to be like my_app_database. I want to rename ddo2ahvosfggq to my_app_database.
How can I do that?
Can I use ALTER DATABASE? http://www.postgresql.org/docs/9.1/static/sql-alterdatabase.html
There is already a question in StackOverflow How to rename database in Heroku?
But the answers is to rename the app. I don't know how renaming app will work?
If my_app is the name of my heroku app. Will the below DATABASE_URL work?
postgres://xxxxxxxxx:xxxxxxxx#xxxxxxxxx.compute-1.amazonaws.com:5432/my_app
You can't. It's a Database-as-a-Service (DBaaS) and database name is not customizable there. You just get it from the service and use. Since it's not a user-facing detail for web applications, pretty names are of no use. So why bother?
Most actions you can do with your database are listed on Heroku Postgres website. You can:
regenerate credentials (that don't include database name, only username and password)
wipe the database (that doesn't rename the database as well, just drops the tables)
create one more database (but it will be named randomly, just as all the others)
You don't ever really need to type in the name of the production database on Heroku.
Heroku has a post commit hook which writes the production DB details into /config/database.yml.
If you ever really need to to query the database without a model you would establish a connection by:
ActiveRecord::Base.establish_connection
query = ActiveRecord::Base.connection.execute('SELECT * FROM foo;')
I recently had a short term project served off of Heroku that approached 10,000 Postgres records and needed to be upgraded from hobby-dev to hobby-basic. Being new to Heroku, I did my best in finding out how to do so and followed heroku's upgrade using PG Backup guide. With a bit of trial and error, I thought I got it working. I erred on the side of caution and didn't do the last deletion of the old DB step. However, I soon got a message from Heroku telling me I've passed the 10,000 record limit. I went to my Heroku dashboard and checked out the DB size. It doesn't look like the Hobby-Basic DB had anything stored in it. I waited a couple weeks and checked again, thinking it may have been a realtime updating issue. Still no tables in the new Hobby-Basic DB.
So my question is, is there a way for me to find out which DB my Heroku app is set up to use currently? How do I force it to switch to a different DB in my app?
This is the guide I followed to upgrade from Hobby-Dev to Basic: https://devcenter.heroku.com/articles/upgrade-heroku-postgres-with-pgbackups
Presumably your app is reading from the environment variable DATABASE_URL. If you have multiple database then you should also have other environment variables like HEROKU_POSTGRESQL_(color). You can access those environment variables from within your app to inspect the configured database information, and you can change them in the Heroku panel. If you change DATABASE_URL to one of the values of HEROKU_POSTGRESQL_(color), then your app should pick up on that and switch to the different database. You can also edit your configuration and access another environment variable while bypassing DATABASE_URL entirely.
I'm confronted with a large number of clients all doing large number of transactions on distinct record sets.
I have say 100 clients each with 500k product records.
Previously I had separated their data with different schemas. It worked fine for a small time then we noticed that when one client was hammering the database all others would grind to a halt.
We then moved to separate instances of Heroku. Each client had their own application and thus their own database. The problem with this is that on-boarding a new client means we have to fire up a new Heroku app and typically that task falls to a developer.
Ok.. so that's the background.
Now my question. I want to create a new database on Heroku for each client. (all in one "application"). When we on-board a new client my app will create the database and tie the username to that database.
I can create a database readily enough with something like this.
heroku addons:add heroku-postgresql:#{db_plan}
But what I don't know is how to get the username and password for this new database, so I can store it against the new user.
QUESTION: How does one get the user name and password for a database they created on heroku?
the heroku toolbelt has a pg:credentials option, I've never tried what you're attempting, but it's possible you can obtain that information here.
Usage: heroku pg
List databases for an app
Additional commands, type "heroku help COMMAND" for more details:
pg:credentials DATABASE # Display the DATABASE credentials.
pg:info [DATABASE] #
pg:kill procpid [DATABASE] # kill a query
pg:killall [DATABASE] # terminates ALL connections
pg:promote DATABASE # Sets DATABASE as your DATABASE_URL
pg:ps [DATABASE] # view active queries with execution time
pg:psql [DATABASE] #
pg:pull <REMOTE_SOURCE_DATABASE> <LOCAL_TARGET_DATABASE> # Pull from REMOTE_SOURCE_DATABASE to LOCAL_TARGET_DATABASE
pg:push <LOCAL_SOURCE_DATABASE> <REMOTE_TARGET_DATABASE> # Push from LOCAL_SOURCE_DATABASE to REMOTE_TARGET_DATABASE
pg:reset DATABASE # Delete all data in DATABASE
pg:unfollow REPLICA # stop a replica from following and make it a read/write database
pg:wait [DATABASE] # monitor database creation, exit when complete
and furthermore...
Usage: heroku pg:credentials DATABASE
Display the DATABASE credentials.
--reset # Reset credentials on the specified database.
Local testing seem to indicate that it does, indeed, give you the database user and password.
In my rails 4 application directory, I typed "heroku pg:credentials DATABASE" into terminal to get all the information about the database for my application which is deployed on heroku. Since I'd like to view the data inside my postgresql database, I tried inputting the information into Induction, but it ended up not responding and I was forced to enter the activity monitor and force quit. I followed the same procedure several more times all with the same result. Is my version of Induction faulty? Should I be using a different program to view my database? Or am I doing something wrong?
I'm new to rails, so thanks for your help!
Yes, you can. Heroku Postgres allows connections from outside Heroku's network. The credentials displayed from heroku pg:credentials will be all you need and should work. Maybe try using the official PGAdmin tool instead?
I'm building a system which allows the user to modify the database.yml contents via an admin frontend interface.
That changes to database.yml obviously don't have any affect until the application is restarted. Rather than forcing the user (who may not have SSH access to the physical box) to restar the application manually, I'd like a way to force ActiveRecord to reload the config post startup.
Rather than requiring them to restart the server, is there a way to force ActiveRecord to re-initialize after initial startup?
Rationale
There are two use cases for this - a) initial setup wizard b) moving from sqlite evaluation database to production supported database.
Initial Setup Wizard
After installing our application and starting it for the first time the user will be presented with a setup wizard, which amongst other things, allows the user to choose between the built in evaluation database using sqlite or to specify a production supported database. They need to specify the database properties. Rather than asking users to edit yml files on the server we wish the present a frontend to do so during initial setup.
Moving from sqlite evaluation database to production supported database
If the user opted to go with the built in evaluation database, but alter wants to migrate to a production database they need to update the database settings to reflect this change. Same reasons as above, a front end rather than requiring the user to edit config files is desired, especially since we can validate connectivity, permissions etc from the application rather than the user finding out it didn't work when they try to start the application and they get an activerecord exception.
Restart your Rails stack on the next request just as you would if you had access to the server.
system("touch #{File.join(Rails.root,'tmp','restart.txt')")
Building on #wless1's answer in order to get ERB-like ENV vars (e.g. <%= ENV['DB_HOST'] %>) working try this:
YAML::load(ERB.new(File.read(File.join(Rails.root, "config/database.yml"))).result)
ActiveRecord::Base.establish_connection config[Rails.env]
Theoretically, you could achieve this with the following:
config = YAML::load File.read(File.join(Rails.root, "config/database.yml"))
ActiveRecord::Base.establish_connection config[Rails.env]
You'd have to execute the reconnection code in every rails process you're running for it to be effective. There's a bunch of dangerous things to worry about here though - race conditions when changing the database config, syntax problems in the new database.yml, etc.