http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL
Dump your tables with
mysqldump --compatible=postgresql databasename > outputfile.sql
but even then you will have to change quote escaping:
sed -ri "s/([^\\])\\\'/\1\'\'/g" outputfile.sql
You also have to manually modify the data types etc. as discussed later.
After you convert your tables, import them the same way you were used to in MySQL, that is
psql -h server -d databasename -U username -f data.sql
[edit]
List of available data types can be reached also by using psql's internal slash command
\dT
.MySQL | PostgreSQL | ANSI Standard SQL | comments |
TINYINT SMALLINT MEDIUMINT BIGINT | SMALLINT SMALLINT INTEGER BIGINT | INTEGER INTEGER INTEGER NUMERIC(20) | see [2]; integer size in PostgreSQL is 4 Bytes signed (-2147483648 – +2147483647) |
TINYINT UNSIGNED SMALLINT UNSIGNED MEDIUMINT UNSIGNED INT UNSIGNED BIGINT UNSIGNED | SMALLINT INTEGER INTEGER BIGINT NUMERIC(20) | INTEGER INTEGER INTEGER NUMERIC(10) NUMERIC(20) | SQL doesn't know UNSIGNED , all numbers are signed. |
FLOAT FLOAT UNSIGNED | REAL REAL | FLOAT4 FLOAT4 | |
DOUBLE | DOUBLE PRECISION | FLOAT8 | |
BOOLEAN | BOOLEAN | BOOLEAN | MySQL Booleans are an alias for TINYINT(1); PostgreSQL doesn't auto-convert numbers into booleans. |
TINYTEXT TEXT MEDIUMTEXT LONGTEXT | TEXT TEXT TEXT TEXT | TEXT TEXT TEXT TEXT | |
BINARY(n) VARBINARY(n) TINYBLOB BLOB MEDIUMBLOB LONGBLOB | BYTEA BYTEA BYTEA BYTEA BYTEA BYTEA | BIT(n) BIT VARYING(n) TEXT TEXT TEXT TEXT | |
ZEROFILL | not available | not available | |
DATE TIME DATETIME TIMESTAMP | DATE TIME [WITHOUT TIME ZONE] TIMESTAMP [WITHOUT TIME ZONE] TIMESTAMP [WITHOUT TIME ZONE] | not available TIME TIMESTAMP TIMESTAMP | |
column SERIAL
equals to:
column BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
or:
column INT DEFAULT SERIAL
equals to:
column INT NOT NULL AUTO_INCREMENT UNIQUE | column SERIAL
equals to:
CREATE SEQUENCE name; CREATE TABLE table ( column INTEGER NOT NULL DEFAULT nextval(name) ); | column SERIAL | Note for PostgresSQL:
SERIAL = 1 – 2147483647
BIGSERIAL = 1 – 9223372036854775807
SERIAL is in fact an entity named SEQUENCE. It exists independently on the rest of your table. If you want to cleanup your system after dropping a table, you also have to
DROP SEQUENCE name . More on that topic...Note for MySQL: column SERIAL PRIMARY KEY
or
column SERIAL, PRIMARY KEY(column)
Will result in having 2 indexes forcolumn. One will be generated by the
PRIMARY KEY constraint, and one by the implicit UNIQUE constraint present in the SERIAL alias. This has been reported as a bug and might be corrected. |
column ENUM (value1, value2, [...]) | column VARCHAR(255) NOT NULL, CHECK (column IN (value1, value2, [...]))
or
CREATE TYPE mood AS ENUM ('sad','ok','happy'); CREATE TABLE person ( current_mood mood ... ) | column VARCHAR(255) NOT NULL, CHECK (column IN (value1, value2, [...])) | PostgreSQL doesn't have the ENUM types prior to 8.3, so you need to simulate it with contraints when using < 8.3. |
Nenhum comentário:
Postar um comentário