Mehmet Ergülcü

Copying a table from PostgreSQL to MySQL with one REST call

· 6 min

gorestpostgresqlmysqldatabase

Sometimes you just need to copy the result of a query from one database into a table of another. Not a migration framework, not a nightly pipeline: one query, one target table. Data Transporter is a small Go service for that. It runs a query on the source and writes the rows to the target with a parameterized insert, in batches, and you drive it with REST calls. It works with PostgreSQL, MySQL, MariaDB, SQL Server and Oracle (and SQLite when you allow it), and all the drivers are pure Go, so it is one binary with no client libraries to install.

Below is a real session. I ran everything on my machine with the Compose file in the repository, which starts the service with a PostgreSQL and a MySQL database to try it on.

Start it

git clone https://github.com/ergulcu/data-transporter
cd data-transporter
docker compose up --build

You do not choose a password. The service makes one up and prints it in the log, together with a note about the key it just created (more on that below):

level=INFO msg="generated a random key for the saved database passwords; keep this file with the data" file=/data/crypto.key
level=WARN msg="no DATATRANSPORTER_PASSWORD; using a generated password for this run" user=admin password=2ICERDLJQ75SNZU3MLS6ZJGZPD
level=INFO msg=listening addr=:8888 drivers=postgres,mysql,mariadb,sqlserver,oracle

Set DATATRANSPORTER_PASSWORD to choose your own. Every endpoint except the health check needs HTTP Basic credentials, and a request without them is refused:

GET /db/configs -> 401
{"type":"about:blank","title":"Unauthorized","status":401,"instance":"/db/configs"}

I made a table of 2,500 people in PostgreSQL (person, with an id and a full_name like Kişi 7) and an empty people table in MySQL.

Save the two connections

A connection is saved once, under a code you choose, and used by name afterwards. The URL has no credentials. The user name and password have their own fields, so the password can be stored encrypted.

POST /db/config
{"code": "source", "driver": "postgres", "url": "postgres://postgres:5432/shop?sslmode=disable",
 "username": "shop", "password": "shop"}
201
{"code":"source","driver":"postgres","url":"postgres://postgres:5432/shop?sslmode=disable",
 "username":"shop","passwordSet":true,"updatedAt":"2026-11-22T20:43:46.428232584Z"}

The answer says passwordSet: true and never includes the password, and neither does any later read. A URL that has a password in it is refused, so that no password is ever stored unencrypted by accident. After saving the MySQL side as target, I tested it:

POST /db/config/target/test -> 200
{"success":true,"message":"Connected","databaseProduct":"MySQL 8.4.13"}

One call to copy

POST /db/migrate
{"sourceCode": "source", "targetCode": "target",
 "sourceQuery": "SELECT id, full_name FROM person",
 "targetQuery": "INSERT INTO people (person_id, name) VALUES (:id, :full_name)"}
200
{"success":true,"message":"Migration completed","transferredRows":2500,"committedRows":2500,"durationMillis":691}

The target query uses named parameters. :id and :full_name are matched to the columns of the source query, ignoring case (Oracle reports its column names in upper case), and turned into the placeholders of the target database. You never write $1 for PostgreSQL and ? for MySQL.

The rows are streamed: each one is written as it arrives, and the result is never held in memory, so the table can be larger than the service's RAM. I checked the target afterwards: 2,500 rows, and the Turkish text came through intact (Kişi 7).

When it fails halfway

This is the part that decides whether a copy tool is trustworthy. I put a row with person_id 2000 in two target tables beforehand, so the copy would fail when it reached row 2000 of 2500, and used the two commit modes.

ALL, the default: everything or nothing.

POST /db/migrate   {..., "commitMode": "ALL", "batchSize": 500}
422
{"success":false,"message":"Migration failed: row 2000: Error 1062 (23000): Duplicate entry '2000' for key 'people_all.PRIMARY'",
 "transferredRows":1999,"committedRows":0,"durationMillis":524}

It got 1,999 rows in and then stopped, but committedRows is 0. The whole copy is one transaction and it was rolled back. The target table has only the one row that was there before.

BATCH: commit as you go.

POST /db/migrate   {..., "commitMode": "BATCH", "batchSize": 500}
422
{"success":false,"message":"Migration failed: row 2000: Error 1062 (23000): Duplicate entry '2000' for key 'people_batch.PRIMARY'",
 "transferredRows":1999,"committedRows":1500,"durationMillis":551}

The same failure, a different outcome: committedRows is 1,500. Rows 1 to 1,500 were committed in three batches of 500, and the fourth batch, which contained the bad row, was rolled back. The target table has 1,501 rows: the 1,500 plus the one that was there before.

Use ALL when a half-copied table is worse than no table. Use BATCH when the transfer is too big for one transaction. And committedRows tells you exactly where you stand in both cases, so you can fix the problem and restart from the right place.

Runs in the background

For a long copy you do not want an HTTP request that waits. POST /db/migrations answers 202 at once with a job:

202
{"id":"c1ad3efb-5cae-4399-9c17-df2f362b5dee","status":"RUNNING","sourceCode":"source","targetCode":"target",
 "transferredRows":0,"startedAt":"2026-11-22T20:43:48.252907768Z","finishedAt":null,"result":null}

and GET /db/migrations/{id} follows it. A moment later:

{"id":"c1ad3efb-5cae-4399-9c17-df2f362b5dee","status":"SUCCEEDED","transferredRows":2500,
 "finishedAt":"2026-11-22T20:43:48.901551875Z","result":{"success":true,"message":"Migration completed","transferredRows":2500,"committedRows":2500,"durationMillis":648}}

The statuses are RUNNING, SUCCEEDED, FAILED and CANCELLED. DELETE /db/migrations/{id} cancels a job, and stopping the service cancels the running ones, which roll back whatever they have not committed.

The saved passwords, and a key you never see

Saved connections survive a restart: they are kept in a small SQLite file in the data directory. Their passwords are encrypted with AES-256-GCM, with a key derived from a password and a salt with PBKDF2.

The question with any such scheme is where that key comes from. The easy answer is a default written in the source code, and I do not like it: everyone who reads the code knows the key. So Data Transporter does not have one. If you give it your own (DATATRANSPORTER_CRYPTO_PASSWORD and DATATRANSPORTER_CRYPTO_SALT, both or neither), it uses that. If you do not, it generates a random key on the first start and keeps it in crypto.key in the data directory, where the next start reads it back, so the saved passwords still open.

I checked what is actually on disk. I saved one more connection with a recognizable password and looked at the raw bytes of the data volume:

-rw-------  crypto.key
-rw-r--r--  datatransporter.db
-rw-r--r--  datatransporter.db-wal

The key file can only be read by its owner. I searched all of the files for the password I had just saved: it is nowhere in them. The connection itself is there, with the password column holding base64 text that looks like noise.

Two consequences are worth knowing. The key file must be backed up together with the data: without it the saved passwords cannot be decrypted, and you would save them again. And if you use your own key and later change it, the same thing happens.

Not everything is the same as before

  • DB2 is not supported. There is no pure Go driver for it: IBM's needs cgo and the DB2 client library.
  • Rows are written one statement at a time inside the transaction. Go's database/sql has no JDBC-style batch, so batchSize controls how often progress is reported and, with BATCH, how often to commit.
  • Connection settings saved by the earlier Java version are not read; save them again.

The integration test moves one set of rows through PostgreSQL, MySQL, SQL Server and Oracle and back to PostgreSQL, and checks that Turkish text, decimals, timestamps, bytes and nulls arrive unchanged.

Source code: github.com/ergulcu/data-transporter

© 2026 Mehmet Ergülcü SvelteKit · Tailwind · <1 KB JS