Ruby & SQLite & Heroku & PostgreSQL - ruby

I've a simple Sinatra application which I've developed using SQLite. The database is a simple two-column table: an ID and a string entry.
I would like to deploy this app to Heroku. What's the least painful way to convert an SQLite database to PostgreSQL, understanding that PostgreSQL is required to deploy to Heroku.

For simple use cases heroku db:push will push your local sqlite database into your Heroku Postgres database.
It's worth considering then switching to using Postgres locally and then use heroku db:pull to bring the database back from Heroku to your new local postgres instance.
I'll caveat to say that whilst heroku db:pull works for SIMPLE databases once you start using more complex Postgres datatypes then you need to use something like heroku pg:transger which is Postgres > Postgres only.

Use the taps gem, as shown in this Railscast.

First you'll want to install Postgres on your local machine so you can make changes and easily deploy to heroku. Second you'll want to migrate from SQLite to Postgres. I just did the migration for my own Rails app following Heroku's instructions and it took less 5 minutes. Seems simple enough.
The instructions to migrate are here (even has instructions on installing Postgres locally). Then you can follow the Heroku getting started guide for the rest.

Related

Heroku: How Can I Stop My Postgres Databases from Disappearing after a few minutes?

Yesterday I uploaded a Rails 5 application with multiple databases to Heroku. I have a hobby-dev postgres add-on. This morning I successfully imported my 7 Postgres databases using pg_dump backups created according to the Heroku documentation.
PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' HEROKU_POSTGRESQL_COLOR_URL
Several times I have imported these databases because after a few minutes they disappear. I ran my Heroku app and the first database was successfully accessed but by the time I tried to access the second one I received a 'relation "xxxx" does not exist'. When I went back to my datastore the databases were gone. When I tried to run my app a second time I got a 'relation "xxxx" does not exist' on the first table that I successfully accessed the previous time I ran my app.
I'm not seeing any errors when I look at the data-store for the database. They just disappear. I checked to see if there was a limit to the number of databases I could have with the hobby-dev but did not see any. The row count is under 10,000. Each time I have imported my pg_dump files I get a warning email about the number of rows.
UPDATE 2/17/2017 10:42 AM central: The only thing I have found so far are some posts stating that the Heroku filesystem is ephemeral, and does not persist between dyno restarts. If this is my problem:
How do I know when dynos restart if I don't restart it? I had not restarted my app when my databases disappeared.
How can I permanently store my databases using the Postgres add-on or do I have to store my databases elsewhere? Surely the add-on has a way to permanently store databases.
I assume you use Heroku PostgreSQL offering (instead of trying to set it up on your own). If that's the case the ephemeral nature of dyno file systems shouldn't be your concern.
I recommend that you first create the seven (empty) databases and see if they disappear or not. You can create a single database with
heroku addons:create heroku-postgresql:hobby-dev
After each call run heroku pg:wait to wait until the database has been provisioned. If the databases don't disappear try restoring your backups then.

Heroku with H2 Database

This is related to running Craig Walls Spring Social Showcase on Heroku. This application runs fine locally using an H2 in memory database. When I deploy to Heroku, however, the database no longer works. Is there something else that needs to be configured on the Heroku server to get the in memory database to run?
To my knowledge there is no H2 support on the Heroku server. So you can't use H2 as an in memory database.
You can get a list of supported databases here
I would recommend you to use Postgres as a database. This is a good starting point.

Some help regarding Postgres on Heroku

i have just started working on an application in PHP. I have configured the Postgres add on in my application on Heroku. But i am still not sure how to start working on the DATABASE. what i mean is that there is a thing called DATACLIP on the POSTGRES DB window on Heroku and when i try to create a table there, it gives me some weird errors.
But no matter what query i write there, it always gives me some error. So, i am not sure whether i can create the Tables and insert my data from DATACLIP or not. and If not, from where can i do this?
Dataclips are only useful for read-only queries. Think gist for SQL and data.
To create data on your database, you can:
Use database migrations that run within the heroku platform itself
Connect to your database using the provided credentials from anywhere (you must use SSL). To get credentials, either run heroku pg:credentials <DATABASE_COLOR> --app <YOUR_APP>, or go to your database in http://postgres.heroku.com. You should be able to use pg_admin or any other postgres front end to connect and create tables.
You can connect directly using the heroku toolbelt (and assuming you have a proper psql installation in your dev box) with heroku pg:psql --app <YOUR_APP>. From there just use SQL to create your data (CREATE TABLE, etc)

How do I connect to a postgres database with Sequel?

I was making a web app to deploy using Heroku.com when I realized that the only database type they support is PostgreSQL. Up until now, my app (powered by the Ruby gem Sinatra) accessed the database via the .Sqlite method for the Sequel gem.
Here's my Ruby script from when I used Sequel to access the .db file via SQLite:
DB = Sequel.sqlite('mydatabase.db')
DB.create_table :mytable do
primary_key :id
String :column_name
end
I installed PostgreSQL after learning Heroku used only that. Here's the script via postgres (my username is literally 'postgress', though I obviously won't reveal my password in this question):
DB = Sequel.postgres('mydatabase.db',:user=>'postgres',:password=>'my_password_here',:host=>'localhost',:port=>5432,:max_connections=>10)
DB.create_table :mytable do
primary_key :id
String :column_name
end
However, when I run this code, I get the following error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/sequel-3.38.0/lib/sequel/adapters/postgres.rb:208:in 'initialize': PG::Error: FATAL: database "mydatabase.db" does not exist (Sequel::DatabaseConnectionError)
I've tried searching Google, StackOverflow, Sequel documents, and the Heroku help documents for any help, but I've found no fix to this problem.
Does anyone know what I am doing wrong?
The database mydatabase.db doesn't exist, as per the error message from Pg. Likely reasons:
You probably meant mydatabase without the SQLite-specific .db filename suffix
It's possible you created the db with different case, eg "Mydatabase.db";
You might be connecting to a different Pg server than you think you are
You never created the database. Unlke SQLite's default behaviour, Pg doesn't create databases when you try to connect to a database that doesn't exist yet.
If in doubt, connect to Pg with psql and run \l to list databases, or connect via PgAdmin-III.
The PostgreSQL documentation and tutorial are highly recommended, too. They're well written and will teach you a lot about SQL in general as well as Pg in particular.
BTW, the postgres user is a superuser. You should not be using it for your application; it's like running your server as root, ie a really bad idea. Create a new PostgreSQL user without superuser, createdb or createuser rights and use that for your application. You can either CREATE DATABASE somedb WITH OWNER myappuser - or preferably, create the database owned by a different user to your webapp user and then expicitly GRANT the webapp user the minimum required permissions. See user management and GRANT.
On heroku all you need to do is tell Sequel to connect to the content of the DATABASE_URL environment variable (which is a properly formed url that Sequel understands):
DB = Sequel.connect(ENV['DATABASE_URL'])

Can't use PSQL with a Heroku shared database

I'm developing an application and hosting it on Heroku. For now, I want to use the free shared database solution.
My problem is that I can't access the database.
$ heroku pg:psql
! Cannot ingress to a shared database
I've read elsewhere that I can't connect to shared databases via psql.
I can't seem to find any information on that on Heroku's Dev Center, so this leaves me with the question - how can I edit or change or do anything with my database?
If you're happy to use a beta addon then there is the Heroku Shared PostgreSQL 9.1 addon (https://addons.heroku.com/heroku-shared-postgresql) which will permit you to ingress to your database.
However, any changes to your database are usually best done with scripted migrations (in the Ruby on Rails world) rather than connecting to your database to make changes.
If you're more familiar with mySQL then there are a number of mySQL addons which permit direct access also with normal mySQL tools.

Resources