Oracle to PostgreSQL: 7 Things to Watch When Moving Data to a Different Database

· DataMint · 8 min read

Production is Oracle, and the new development environment is going to be PostgreSQL. You moved the table structures, loaded the data, and it finished without errors. A few days later a tester asks, "Why does this customer's address show up as a space instead of being empty?"

What is frightening about moving to a different database is not errors. Errors are visible, so you fix them. The real problem is values that change without an error. The seven items below are things we ran into one by one while building the feature that moves data between Oracle, PostgreSQL, MySQL, MariaDB and SQL Server.

1. Empty strings and NULL

The best-known problem, and one that catches people who already know about it. Oracle stores a zero-length string as NULL. Other databases distinguish the two.

-- Oracle
INSERT INTO t (memo) VALUES ('');
SELECT COUNT(*) FROM t WHERE memo IS NULL;   -- 1

-- PostgreSQL
INSERT INTO t (memo) VALUES ('');
SELECT COUNT(*) FROM t WHERE memo IS NULL;   -- 0

Going from Oracle to another database is quiet. The value was NULL to begin with, so it goes as NULL. The other direction is the problem. If a NOT NULL column in PostgreSQL or MySQL holds empty strings, they cannot be inserted into Oracle. Applications are affected too. Code that queried with memo = '' finds no rows at all in Oracle.

2. Dates: DATE with a time in it, and 0000-00-00

Oracle's DATE, despite its name, holds hours, minutes and seconds. PostgreSQL's date holds only the date. Map them as is because the type names match, and the time is cut off. No error is raised. An Oracle DATE has to be received as timestamp.

On the MySQL side there is a date called 0000-00-00. In older versions it was widely used to leave a date blank. Other databases have no such date. Of everything we found in testing, this value was the most unsettling. In our first implementation, this date went into PostgreSQL as 0001-01-01. With no error and no warning, it became January 1 of the year 1 AD.

3. Character length: bytes or characters

Oracle's VARCHAR2(20) is 20 bytes under the default setting. If the character set is UTF-8, one Korean (Hangul) character is 3 bytes, so only six characters fit. If the same database uses a KSC5601 family character set, one character is 2 bytes and ten characters fit. PostgreSQL's varchar(20) is always 20 characters.

Inserting '가나다라마바사', seven Hangul characters
Column definitionDatabase character setResult
Oracle VARCHAR2(20 BYTE)KO16MSWIN94914 bytes, fits
Oracle VARCHAR2(20 BYTE)AL32UTF821 bytes, fails
PostgreSQL varchar(20)UTF87 characters, fits

When going to a database with a different character set, lengths have to be converted to a character basis when the tables are created. And some characters do not exist in one of the character sets. Emoji and some Chinese characters are not in MS949, so they become ? when moved. This also passes without an error.

4. Numbers: NUMBER without precision

In Oracle it is common to declare NUMBER without precision. Integers and decimals both go in. If the receiving side makes it integer, decimals are cut off, and if it makes it double, amount calculations pick up rounding errors. Even if the actual values all look like integers, if the definition does not say so, receiving it as numeric is the safe choice.

Booleans belong here too. PostgreSQL has boolean, and Oracle has long made do with NUMBER(1) or a Y/N in CHAR(1). Which way to align has to be decided by looking at how the application reads that column.

5. Fixed-length CHAR and trailing spaces

Put 'ABC' into CHAR(10) and seven spaces are added at the end. How those spaces are treated in comparisons differs by database. If the migration tool trims the spaces and inserts into VARCHAR, comparisons that worked in the original code may stop working, and in the other direction, making a VARCHAR into CHAR creates spaces that were not there.

We made this mistake at first too. We found that a PostgreSQL varchar was being created as CHAR in MySQL, losing trailing spaces, in a test that moves data between all five databases in every direction. The cause was judging only by the letters in the type name.

6. Sequences and auto-increment

All the data went in, but creating a new record on a screen gives "duplicate key." The sequence that assigns numbers starts from 1. The table already goes up to 500,000.

-- PostgreSQL: after loading, set the sequence to the current maximum
SELECT setval(pg_get_serial_sequence('customer', 'id'),
              (SELECT COALESCE(MAX(id), 1) FROM customer));

Oracle sequences, MySQL's AUTO_INCREMENT, SQL Server's IDENTITY, and PostgreSQL's IDENTITY and serial all work differently. Inserting the original values into an auto-increment column needs a different option in each database, and after inserting, the next number has to be set.

7. Names: case and length

Oracle stores unquoted names in uppercase, PostgreSQL in lowercase. If the migration tool creates Oracle's CUSTOMER with the quotes, select * from customer in PostgreSQL says "table does not exist." Names have to follow the conventions of the receiving database.

Length limits differ too. For constraint and index names, MySQL allows 64 characters, PostgreSQL 63, and Oracle, depending on the version, used to allow 30. And in PostgreSQL an index name has to be unique within the schema. Move an Oracle schema where every table has an index called IDX_01 as is, and it fails at the second table.

After moving, count

Even with all seven taken care of, verification is a separate step. The most basic check is row counts. For each table, see whether the number of rows read equals the number of rows loaded. If they differ, you need to be able to tell how many rows were left out and why.

Checklist for moving to a different database
ItemWhat to check
Empty stringsAre you going from a database that distinguishes '' and NULL to one that does not?
DatesIs the time being cut off? Are there values like 0000-00-00?
Character lengthIs the length in bytes or characters? Are missing characters turning into ?
NumbersIs a number without precision being received into a narrow type?
CHARAre trailing spaces appearing or disappearing?
Auto-incrementIs the next number correct after loading?
NamesCase, length, name collisions
Row countsDo rows read and rows loaded match for each table? If not, is the reason recorded?

In DataMint Core

DataMint Core replaces personal data while moving data between Oracle, MySQL, MariaDB, SQL Server and PostgreSQL. Tables, keys and indexes on the receiving side are created the same as production, while types and names are fitted to the receiving database.

Empty values are handled like this. NULL is NULL in either direction. Other empty values are loaded as the value with the same meaning in the receiving database. If there is no value with the same meaning, only that row is left out, and the column, reason, count and key are recorded in the job log. The whole job is not failed.

DataMint Core run detail screen showing rows read, rows loaded and rows that could not be loaded for each table
Run detail. The rows read and rows loaded are recorded for each table, and rows that could not be loaded because of their values are reported as counts by reason. (The console is shown in Korean. The data on screen is demo data.)
  • Auto-increment columns are loaded with their original values, and the next number is set after loading.
  • Foreign keys are created after all tables are loaded.
  • For lengths, it looks at the source database's character set to decide whether they are in bytes or characters.

Closing

The difficulty of moving to a different database is not in syntax differences but in places where the same word means something different. The empty string, DATE and VARCHAR(20) exist on both sides but mean different things. Before moving, run a query for each of these seven, and after moving, start by counting rows.

If you also need to replace personal data while moving, it is worth reading how to change values while keeping table relationships.

  • #Oracle to PostgreSQL migration
  • #heterogeneous databases
  • #data migration
  • #database migration checklist
  • #test data
  • #cross-database data transfer

Reviewing how you handle test data?

Tell us how you do it today and what databases you run, and we will look at the options with you.

More posts