Database.Options has no way to specify a target database name, and Postgres DSN omits dbname
- Dominant language
- Go
- Stars
- 223
- Forks
- 225
- Avg merge
- 7d 1h
- Merged PRs (30d)
- 1
Description
### Current Behavior
In database/database.go, the Options struct used to configure a database connection has no Database/DBName field:
type Options struct {
Username string `json:"username,omitempty"`
Host string `json:"host,omitempty"`
Port string `json:"port,omitempty"`
Password string `json:"password,omitempty"`
Filename string `json:"filename,omitempty"`
Engine string `json:"engine,omitempty"`
Logger logger.Handler
}
As a result, the Postgres DSN is built without a dbname parameter:
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port)
With no dbname given, the driver falls back to the connecting username as the database name, which fails whenever the username and the actual target database differ — the common case in real deployments.
Notably, error.go already defines ErrNoneDatabase ("No Database selected" / "database name is empty"), but nothing in New() ever triggers it for the Postgres path, since there's no field to check.
### Expected Behavior
Options should include a Database field, wired into the DSN as dbname=%s (and ideally sslmode=%s), so callers can target a specific database. If it's left empty, New() should return ErrNoneDatabase rather than silently falling back to the username.
### Screenshots/Logs
Reproduced by calling database.New() directly with a Postgres container where the connecting user (testuser) and the actual database (actualdb) have different names:
[error] failed to initialize database, got error failed to connect to `user=testuser database=`:
127.0.0.1:5432 (localhost): server error: FATAL: database "testuser" does not exist (SQLSTATE 3D000)
### Suggested fix
Add a Database string field to Options, wire it into the DSN as dbname=%s, and consider returning ErrNoneDatabase when it's empty.
### Environment
- **Host OS:** Windows
- **Platform:** Docker
---
Contributor guide
Research direction
Start in database/database.go by reading Options and New(), then check error.go for ErrNoneDatabase. Trace the Postgres DSN construction and verify the Database value is included and an empty value produces the documented error. Reproduce the issue with the described Postgres container setup and confirm connections target the specified database.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, postgresql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100