Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switching from MySQL to PostgreSQL for Ruby on Rails for the sake of Heroku

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.

like image 991
Ringo Blancke Avatar asked Jan 05 '12 00:01

Ringo Blancke


People also ask

Can we convert MySQL database to PostgreSQL?

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.

How do I migrate from MySQL to PostgreSQL?

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.

Does MySQL work with Heroku?

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 .


2 Answers

Common issues:

  1. GROUP BY behavior. PostgreSQL has a rather strict GROUP BY. If you use a GROUP BY clause, then every column in your SELECT must either appear in your GROUP BY or be used in an aggregate function.
  2. Data truncation. MySQL will quietly truncate a long string to fit inside a char(n) column unless your server is in strict mode, PostgreSQL will complain and make you truncate your string yourself.
  3. Quoting is different, MySQL uses backticks for quoting identifiers whereas PostgreSQL uses double quotes.
  4. LIKE is case insensitive in MySQL but not in PostgreSQL. This leads many MySQL users to use LIKE as a case insensitive string equality operator.

(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.
  • Raw SQL (including any snippets in where calls).
  • String length validations in your models.
  • All uses of LIKE.
like image 164
mu is too short Avatar answered Sep 28 '22 08:09

mu is too short


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.

like image 25
Dylan Markow Avatar answered Sep 28 '22 07:09

Dylan Markow