initdb fails with invalid byte sequence for encoding UTF8 when the installation path has non-ASCII characters

Domain: windows · Evidence E1 — Maintainer reviewed
windowspostgresqlinitdbgbkutf8non-ascii-path

Summary

On Windows with system locale `zh-CN` (ANSI code page **936 / GBK**) and a user directory

Problem

On Windows with system locale zh-CN (ANSI code page 936 / GBK) and a user directory whose name contains non-ASCII characters (C:\Users\<non-ASCII-user>) — an embedded PostgreSQL 18.1 shipped by pg0 never starts. Every session reports only a connection failure:

reflect failed … connect ECONNREFUSED 127.0.0.1:9077
Daemon Failed (Timeout) (coding-agent @ :9077)

The real error exists only in the profile log (~/.hindsight/profiles/coding-agent.log):

RuntimeError: Failed to start embedded PostgreSQL after 5 attempts. Last error:
Error: initdb failed: FATAL: invalid byte sequence for encoding "UTF8": 0xb7
initdb: 无法创建目录 "C:/Users/<non-ASCII-user>/.pg0/instances/hindsight-embed-coding-agent/data"

Two things make this expensive to diagnose:

Root Cause

During bootstrap, initdb passes paths as argument strings to the postgres process. At least one of them is the installation tree: -L <install dir>/share, here C:\Users\<non-ASCII-user>\.pg0\installation\18.1.0\share.

pg0.exe produces those bytes in the system ANSI code page (936 / GBK). The first non-ASCII character of that path is a CJK character, U+98CE, and GBK encodes it as the two bytes 0xB7 0xE7. PostgreSQL 18 requires its arguments to be valid UTF-8, so bootstrap postgres rejects the string immediately:

FATAL: invalid byte sequence for encoding "UTF8": 0xb7

0xb7 is exactly the first byte of that character in GBK — the error is quoting the offending byte. The chain is reproducible in one line:

'\u98ce'.encode('gbk') == b'\xb7\xe7'   # True — 0xb7 is the leading byte

The home directory is shown as <non-ASCII-user> rather than the account name it actually was: the byte chain depends on the character's code point, not on which account owns the directory, and a published lesson should not carry someone's home path.

The consequence is the part worth remembering: the failure is not "the path cannot be reached", it is "a non-ASCII path was handed over in the wrong encoding". So every fix aimed at path access fails:

attemptmeasured result
pass a pure-ASCII data directory (--data-dir C:\pg0data)same 0xb7 error; initdb: removing contents of data directory
override USERPROFILE and junction ~/.hindsight, ~/.pg0the server still resolves C:/Users/<non-ASCII-user>/.pg0/... — pg0 uses Windows known folders (SHGetKnownFolderPath) and ignores USERPROFILE
configure pg0 to relocate its installationpg0.exe exposes only --data-dir; the embedding API passes only name/port/username/password/config; no PG0_* variable appears in the binary

As long as the installation tree sits under a non-ASCII path, initdb fails — the data directory location is irrelevant.

Solution

Three routes were verified on the affected machine.

Step 1 (do this first): let the dependency download finish

Install uv and make sure uv/uvx are on PATH — without them the daemon does not get as far as the start sequence. Then run the daemon start more than once so the ~3.8 GB of dependencies finish downloading. Only then is the visible error the real one. Until then you will chase a timeout that is a side effect.

Step 2: pick one of three real fixes

Route A — bypass pg0 and point the daemon at an external PostgreSQL. Usually the least work and it survives plugin restarts. The daemon forwards every HINDSIGHT_API_* variable to the child process, so setting them as user-level environment variables persists. The external database may still need extensions; pg0 ships install-extension / list-extensions (pgvector among them).

HINDSIGHT_API_DATABASE_URL=postgresql://<user>:<password>@127.0.0.1:<port>/<db>

Route B — put the whole PostgreSQL tree on a pure-ASCII path. Move the entire installation, for example to C:\pg0inst, or use a Windows account whose name is ASCII. This works only if both the installation tree and the data directory are ASCII: pg0's installation directory is fixed at ~/.pg0 through the known-folder API, so "change the environment variable and add a junction" — the intuitive shortcut — does not move it.

Route C — use a cloud backend. In ~/.hindsight/coding-agent.json, set serverMode=cloud with an apiUrl and token, so no local database runs at all. This needs an API token from the cloud vector service.

Verification

Reproduce on a Windows account whose name contains non-ASCII characters:

hindsight-embed -p coding-agent daemon start
Select-String -Path "$env:USERPROFILE\.hindsight\profiles\coding-agent.log" `
  -Pattern 'invalid byte sequence for encoding "UTF8": 0xb7'

A match means reproduced.

Prove the data directory is not the cause (this is the experiment that separates the two hypotheses):

pg0.exe start --name t --data-dir C:\pg0data -u postgres -P postgres -n postgres

If the data directory is already pure ASCII and the same byte error appears, the data directory is not the cause.

Confirm the fix:

hindsight-embed -p coding-agent daemon status
Invoke-WebRequest http://127.0.0.1:9077/health -UseBasicParsing | Select-Object StatusCode

status reports running and the health request returns 200; Hindsight then returns real results instead of ECONNREFUSED.

Notes

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons