# Mysql to postgres rake task

**URL:** <https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138>\
**Category:** Rails\
**Created:** [May 16, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138 "2009-05-16T18:12:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 16, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/1 "2009-05-16T18:12:01Z")

</div>

Hey all. I’m trying to export my app’s data from mysql to postgres in  
advance of a database switch.

I’ve tried this:

task(:export =\> :environment) do  
@users=User.find(:all)  
ActiveRecord::Base.establish\_connection(:pg)  
for user in @users  
user.save\_with\_validation(perform\_validation = false)  
end  
ActiveRecord::Base.establish\_connection(:development)  
end

However, nothing’s happening – no activity in the pg environment log,  
no records added.

I’ve also tried exporting as yaml and then importing, using Tobias  
Lutke’s approach ([http://blog.leetsoft.com/files/code/backup.rake](http://blog.leetsoft.com/files/code/backup.rake))

However, the import bombs here:  
myFile=YAML.load\_file(“#{tbl}.yml”)  
because of some unexpected quote marks in some of the content.

Any help getting either of these approaches to work would be hugely  
appreciated.

---

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 16, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/2 "2009-05-16T19:22:59Z")

</div>

On Sat, May 16, 2009 at 9:11 AM, magic\_hat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hey all. I’m trying to export my app’s data from mysql to postgres in  
> advance of a database switch.

Check out the yaml\_db plugin:  
[http://agilewebdevelopment.com/plugins/yamldb](http://agilewebdevelopment.com/plugins/yamldb)

## FWIW,

Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 17, 2009, 4:30am UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/3 "2009-05-17T04:30:37Z")

</div>

Hassan,

the yaml\_db plugin looked promising, but it’s bombing because of  
unbalanced quotes in some of the content, much like the code from  
Tobias that I posted.

On May 16, 12:06 pm, Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 17, 2009, 4:31am UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/4 "2009-05-17T04:31:47Z")

</div>

Michael,

the perl script looks great, but I’m getting a segmentation fault  
error when I run, perhaps because some of the content involves long  
news articles.

---

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 16, 2009, 10:19pm UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/5 "2009-05-16T22:19:59Z")

</div>

On Saturday 16 May 2009, magic\_hat wrote:

> Hey all. I’m trying to export my app’s data from mysql to postgres in  
> advance of a database switch.

Try exporting and importing YAML first. If that doesn’t work, you may  
have better luck with mysqldump + mysql2pgsql[\*] + psql.

Michael

[\*] [http://pgfoundry.org/projects/mysql2pgsql/](http://pgfoundry.org/projects/mysql2pgsql/)

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 17, 2009, 9:42am UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/6 "2009-05-17T09:42:51Z")

</div>

If it just the unbalanced quotes that are causing the problem you could  
possibly first convert all the quotes to some escape sequence then  
transfer  
the database, and then convert the escape sequences back again.

Colin

2009/5/17 magic\_hat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 17, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/7 "2009-05-17T16:27:11Z")

</div>

magic\_hat wrote:

> Hey all. I’m trying to export my app’s data from mysql to postgres in  
> advance of a database switch.
> 
> I’ve tried this:
> 
> task(:export =\> :environment) do  
> @users=User.find(:all)  
> ActiveRecord::Base.establish\_connection(:pg)  
> for user in @users  
> user.save\_with\_validation(perform\_validation = false)  
> end  
> ActiveRecord::Base.establish\_connection(:development)  
> end
> 
> However, nothing’s happening – no activity in the pg environment log,  
> no records added.

Did you look at this plugin?

[http://agilewebdevelopment.com/plugins/manage\_fixtures](http://agilewebdevelopment.com/plugins/manage_fixtures)

[%] rake db:fixtures:export\_all RAILS\_ENV=development  
[%] rake db:fixtures:load

When loading specify your new database, RAILS\_ENV=new\_development or  
other.

Stephan

> I’ve also tried exporting as yaml and then importing, using Tobias  
> Lutke’s approach ([http://blog.leetsoft.com/files/code/backup.rake](http://blog.leetsoft.com/files/code/backup.rake))
> 
> However, the import bombs here:  
> myFile=YAML.load\_file(“#{tbl}.yml”)  
> because of some unexpected quote marks in some of the content.
> 
> Any help getting either of these approaches to work would be hugely  
> appreciated.

---

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 17, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/8 "2009-05-17T17:32:32Z")

</div>

> ```
> end
> myFile=YAML.load_file("#{tbl}.yml")
> 
> ```
> 
> because of some unexpected quote marks in some of the content.
> 
> Any help getting either of these approaches to work would be hugely  
> appreciated.

Taps ([http://adam.blog.heroku.com/past/2009/2/11/](http://adam.blog.heroku.com/past/2009/2/11/)  
taps\_for\_easy\_database\_transfers/) is an excellent solution for this.  
Heroku use it for cross-db support. Note that it works over network  
connections, which is very handy.

---

<div class="post-metadata">

**Author:** ![magic\_hat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@magic\_hat](https://www.ruby-forum.com/u/magic_hat)\
**Post date:** [May 17, 2009, 4:40pm UTC](https://www.ruby-forum.com/t/mysql-to-postgres-rake-task/168138/9 "2009-05-17T16:40:18Z")

</div>

On Sat, May 16, 2009 at 7:29 PM, magic\_hat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the yaml\_db plugin looked promising, but it’s bombing because of  
> unbalanced quotes in some of the content

ouch. Can you post the stack trace and an example data row that’s  
causing the problem?

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid
