Golang使用env文件时database/sql数据库连接失败问题排查
The Problem
Originally, your Go code with hardcoded Postgres connection parameters worked perfectly. But when you switched to using os.Getenv("DATABASE_PWD") to fetch the password from environment variables, you hit this error:
panic: pq: password authentication failed for user "bond"
Common Causes & Fixes
Let’s walk through the most likely issues and how to fix them, ordered by how often they pop up:
Environment variable isn’t loaded properly
This is the #1 culprit. If you set the variable in your terminal but didn’t run your Go program in the same session, or you’re using a.envfile without a loader,os.Getenvwill return an empty string—leading to an incorrect password.- Fixes:
- If setting the variable temporarily in your terminal, run your program right after in the same session:
export DATABASE_PWD="password" go run your-file.go - If using a
.envfile, you need a library likegithub.com/joho/godotenvto load it (Go’s standard library doesn’t auto-read.envfiles). Example:import ( "log" "github.com/joho/godotenv" ) func main() { // Load variables from .env err := godotenv.Load() if err != nil { log.Fatalf("Failed to load .env file: %v", err) } // Now fetch the password password := os.Getenv("DATABASE_PWD") // Rest of your connection code... }
- If setting the variable temporarily in your terminal, run your program right after in the same session:
- Fixes:
Typos in the environment variable name
Double-check that the variable name in your.envfile matches exactly what you’re passing toos.Getenv. A tiny typo (likeDATABASE_PASSWORDinstead ofDATABASE_PWD, or mixed case) will result in an empty string being returned.- Fix: Cross-verify the variable name in both your
.envfile and your Go code—they need to be identical.
- Fix: Cross-verify the variable name in both your
Special characters in the password aren’t handled correctly
If your password has special characters like$,@, spaces, or quotes, setting it directly in the terminal or.envfile can cause parsing issues, leading to an incorrect password being passed to Postgres.- Fixes:
- When setting the variable in the terminal, wrap the password in quotes:
export DATABASE_PWD="my$tr0ngP@ssword" - In your
.envfile, also wrap the password in quotes to preserve special characters:DATABASE_PWD="my$tr0ngP@ssword"
- When setting the variable in the terminal, wrap the password in quotes:
- Fixes:
Existing environment variable is overriding your value
It’s possible a system-wide or session-wide variable with the same name already exists, andos.Getenvis pulling that instead of your intended value.- Fix: Add a debug print to confirm what password your code is actually fetching (remove this before deploying!):
If this prints an empty string or the wrong value, you’ll know where to focus your troubleshooting.func main() { password := os.Getenv("DATABASE_PWD") fmt.Printf("Retrieved password: '%s'\n", password) // Debug check // Rest of your connection logic... }
- Fix: Add a debug print to confirm what password your code is actually fetching (remove this before deploying!):
Quick Validation Step
Before diving into fixes, always add that debug print to confirm the password variable’s value. This instantly tells you if the issue is with fetching the variable or something else.
内容的提问来源于stack exchange,提问作者Rohit Singh

