PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 25th, 2001, 11:26 AM
JeffCT JeffCT is offline
Dev
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2001
Posts: 1,436 JeffCT User rank is Sergeant Major (2000 - 5000 Reputation Level)JeffCT User rank is Sergeant Major (2000 - 5000 Reputation Level)JeffCT User rank is Sergeant Major (2000 - 5000 Reputation Level)JeffCT User rank is Sergeant Major (2000 - 5000 Reputation Level)JeffCT User rank is Sergeant Major (2000 - 5000 Reputation Level)JeffCT User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 h 52 m 59 sec
Reputation Power: 40
Converting to Postgres from MySQL

What all do I need to think about? I'm writing an app that I want to work with both database types and need to know what exactly must be done because for the most part it's written for mysql.

How do I change the schema? Do I simply add foreign keys? Would that change the way the actual queries look?

And as far as executing queries, do I just use pg_exec() (in PHP) or do I need to start and end transactions every time i want to do a query?

Reply With Quote
  #2  
Old February 19th, 2002, 03:57 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
I have been coming across these issues myself lately. FWIW, here are some of my experiences.

Converting from MySQL to PostgreSQL is not a painless procedure unless your database is very simple.

Generally, you can't just do a mysqldump, and use those SQL INSERT statements to create your PostgreSQL database. But, there are a couple of tools that will help somewhat (YMMV): http://freshmeat.net/projects/my2pg/
http://sourcepole.com/sources/software/mysql2pgsql/

These are Perl scripts that will parse a MySQL dumpfile, and create a PostgreSQL-compatible set of SQL statements for table creation and population.

What I found was that I had to take the MySQL dump and delete all reference to table types, such as TYPE=MyISAM, or TYPE=HEAP, because these were not removed.

Here were my biggest problems:

I.

PostgreSQL handles incrementing id keys completely differently from MySQL. These scripts attempt to simply replace the MySQL method with the PostgreSQL one (a column of type SERIAL automatically creates a sequence) This works fine if you are just creating structure. But, if you want to populate your tables from the MySQL database, you will be explicitly inserting your keys, instead of letting the database do it. (otherwise, your relations are lost). PostgreSQL will complain because it will want to insert based on the NEXTVAL of the sequence for that column. So... 1) take out all references to 'autoindex' from your MySQL dump, then run your conversion tool. This will create those columns as simple int(4). Import your data, make sure your columns are unique, then create your sequence, and set your default value for that column to be NESTVAL for that sequence. Obviously, you will have to define your sequence to start at a number greater than your last MySQL id key

II

PostgreSQL date columns are stricter than MySQL's. MySQL will allow you to have a date such as 2000-00-00, which is not really a date. PostgreSQL will not. MySQL will allow you to have 2000-02-31 (31 days in February), while PostgreSQL will not.

III.

What I ended up doing was separating the table structure creation from the data importing. I massaged the MySQL dump, ran it through mysql2pgsql, examined and fixed small errors in the PostgreSQL data structure file, then piped it into psql. I then ran the MySQL data dump through my2pq, and managed to get most of my data imported properly.

Quite a chore...

In answer to your other questions: you do not need to use transactions in PostgreSQL unless you want to. Ditto for key constraints, etc... So you can import your database, get it running, and then work on adding the other parts. Of course you will find that you have to track down and fix certain key violations from MySQL before you can create your foreign keys ;-).

Foreign key constraints do not change the way your SQL statements look, they just don't allow a "bad" query to corrupt the key structure. There are several types of actions that you can specify as a result of a bad query.

Besides foreign keys and transactiosn, a few basic features of PostgreSQL you will be relieved to finally have are Views, subqueries, and SELECT INTO.

Hopefully, the tools to migrate will evolve. I'm going to try to get a Perl guru friend of mine to take a whack at some of it.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Converting to Postgres from MySQL

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap