I'm trying to push a brand new Ruby on Rails app to Heroku. Currently, it sits on MySQL. It looks like Heroku doesn't really support MySQL and so we are considering using PostgreSQL, which they DO support.
How difficult should I expect this to be? What do I need to do to make this happen?
Again, please note that my DB as of right now (both development & production) are completely empty.
Select PostgreSQL from the list of supported target DBMS, when you are done click on the Ok button. The database management system now is successfully changed from MySQL to PostgreSQL. It's important to note that the "Change DBMS" feature will only convert data types.
CREATE EXTENSION command to create a MySQL Foreign Data Wrapper extension to the PostgreSQL host. CREATE SERVER command to define a connection to the MySQL Server. CREATE USER MAPPING command to define a mapping that associates a Postgres role with the server.
Heroku does not offer a native MySQL add-on but instead supplies it through a third party, ClearDB. If it has not been added to your application already, you can install it from the Heroku Add-Ons Page. Once installed, it will appear in your Add-Ons list in your Resources tab as ClearDB MySQL .
Common issues:
char(n)
column unless your server is in strict mode, PostgreSQL will complain and make you truncate your string yourself.(1) will be an issue if you use AR's group
method in any of your queries or GROUP BY in any raw SQL. Do some searching for column "X" must appear in the GROUP BY clause or be used in an aggregate function
and you'll see some examples and common solutions.
(2) will be an issue if you use string columns anywhere in your application and your models aren't properly validating the length of all incoming string values. Note that creating a string column in Rails without specifying a limit actually creates a varchar(255)
column so there actually is an implicit :limit => 255
even though you didn't specify one. An alternative is to use t.text
for your strings instead of t.string
; this will let you work with arbitrarily large strings without penalty (for PostgreSQL at least). As Erwin notes below (and every other chance he gets), varchar(n)
is a bit of an anachronism in the PostgreSQL world.
(3) shouldn't be a problem unless you have raw SQL in your code.
(4) will be an issue if you're using LIKE anywhere in your application. You can fix this one by changing a like b
to lower(a) like lower(b)
(or upper(a) like upper(b)
if you like to shout) or a ilike b
but be aware that PostgreSQL's ILIKE is non-standard.
There are other differences that can cause trouble but those seem like the most common issues.
You'll have to review a few things to feel safe:
group
calls.where
calls).If you have no data to migrate, it should be as simple as telling your Gemfile
to use the pg
gem instead, running bundle install
, and updating your database.yml
file to point to your PostgreSQL databases. Then just run your migrations (rake db:migrate
) and everything should work great.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With