This afternoon I published two Japanese blog posts to my app's Postgres database. A minute later the public list showed both titles as unreadable characters. The content was fine on disk. It broke on the way into the database.
The cause was a single shell redirect on Windows. Here is what happened and how I changed the workflow.
The setup
My blog posts live as Markdown files. To publish, I generate an INSERT statement with Python and run it with the Supabase CLI:
python - <<'EOF' > publish.sql # read the Markdown file (UTF-8), build an INSERT statement, print it EOF supabase db query --linked -f publish.sql The Markdown files were read as UTF-8. The SQL file looked fine when I printed it in the same terminal. The database still received broken text.
Why it broke
On Windows, when Python's standard output is redirected to a file, it does not use UTF-8 by default. It uses the system's ANSI code page. On a Japanese Windows install, that is cp932 (Shift_JIS).
So the chain was:
- Python read the Markdown as UTF-8. The text in memory was correct.
-
print()wrote it to a redirected stdout, encoded as cp932. - The CLI read
publish.sqlas UTF-8 and sent cp932 bytes to Postgres as if they were UTF-8.
Every step worked as designed. The mismatch was between step 2 and step 3.
It looked fine in the terminal because the terminal and the redirect use different encodings. Printing to the console worked; writing to a file did not.
The fix
Stop relying on stdout for anything that ends up in a file. Write the file explicitly with the encoding you mean:
from pathlib import Path sql = build_insert_statement(markdown_text) Path("publish.sql").write_text(sql, encoding="utf-8") Other options also work, such as running Python in UTF-8 mode (python -X utf8) or setting PYTHONIOENCODING=utf-8. I prefer the explicit write_text, because it keeps working no matter which flags or environment the script runs with.
Repairing the rows
The broken rows were already public, so speed mattered more than elegance. I did not delete them. Deleting would have changed their IDs and broken any links that had already been shared. Instead, I regenerated the statements as UPDATE ... WHERE id = '<id>' from the same Markdown files, wrote them with encoding="utf-8", and re-ran them.
Then I checked three things from the database, not from my terminal:
select id, title, left(content, 40) as head, strpos(content, chr(13)) as has_cr from blog_posts where id in ('<id-1>', '<id-2>'); - The title and first line read correctly.
- The tags were correct.
-
has_crwas0. The Markdown had been saved with CRLF line endings, so I normalized those to LF in the same pass.
Finally, I opened both public pages and confirmed they returned HTTP 200 with the right text.
Takeaways
- On Windows, a redirected stdout uses the system code page, not UTF-8. Do not let
print()produce files that another tool will read. - Write files with an explicit
encoding="utf-8"and check line endings while you are there. - To fix a bad row, update it in place by ID. That way existing links keep working.
- Verify from the system of record, the database itself. A terminal that can display the text proves very little.
Three minutes of mojibake was a cheap lesson. The SQL generator now writes its own file, and the redirect is gone.


