I recently needed to get Postgres MCP Pro setup with Opencode. The biggest issue I kept running into was that the DATABASE_URI environment variables was not being detected from my local .env file.
The confusion stemmed from Opencode docs which vaguely suggested that environment variables declared in .env could be referenced in the MCP configuration like this:
"mcp": {
"postgres": {
"type": "local",
"command": ["uvx", "postgres-mcp", "--access-mode=restricted"],
"enabled": true,
"environment": {
"DATABASE_URI": "{env:DATABASE_URI}"
}
}
}This unfortunately does not work.
The fix for this was to use a file instead. I created a .secrets folder in my repo root and placed a .txt file inside it with the full DATABASE_URI defined. Then I referenced the file like this
"mcp": {
"postgres": {
"type": "local",
"command": ["uvx", "postgres-mcp", "--access-mode=restricted"],
"enabled": true,
"environment": {
"DATABASE_URI": "{file:.secrets/dev-db-conn-str.txt}"
}
}
}This worked and the MCP was successfully connected.
I think the confusion was that the {env:DATABASE_URI} only works for environment variable that are set in OpenCode settings, not within the codebase. This behavior changed back in September 2017. See: https://github.com/anomalyco/opencode/issues/2033#issuecomment-3301312732.