Skip to content

fix(database): add dbname parameter to postgres dsn#970

Open
sm-28601 wants to merge 5 commits intomeshery:masterfrom
sm-28601:fix-dbname-dsn
Open

fix(database): add dbname parameter to postgres dsn#970
sm-28601 wants to merge 5 commits intomeshery:masterfrom
sm-28601:fix-dbname-dsn

Conversation

@sm-28601
Copy link
Copy Markdown

@sm-28601 sm-28601 commented Apr 7, 2026

Description

This PR addresses the issue where the PostgreSQL DSN connection string in database/database.go was constructed without the dbname parameter. Without explicitly defining the dbname, PostgreSQL defaults to connecting to a database matching the operating user's name, which prevents the application from connecting to a dedicated database (e.g., mesherydb).

To fix this:

  1. Added a Database field to the Options struct.
  2. Updated the fmt.Sprintf for the DSN under the POSTGRES case to include dbname=%s.

This PR fixes #969

Signed commits

  • Yes, I signed my commits.

Copilot AI review requested due to automatic review settings April 7, 2026 06:38
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Signed-off-by: Sahil Maurya <sahilmaurya5611@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the database connection options and PostgreSQL DSN construction so the application can explicitly target a configured database name instead of relying on PostgreSQL defaults.

Changes:

  • Added a Database field to database.Options.
  • Updated the PostgreSQL DSN format string to include dbname=%s.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

switch opts.Engine {
case POSTGRES:
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port)
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s dbname=%s", opts.Host, opts.Username, opts.Password, opts.Port, opts.Database)
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opts.Database is interpolated unconditionally into the Postgres DSN. If Options.Database is left empty, the DSN will contain dbname= with an empty value, which is unlikely to be intended. Consider validating opts.Database (return a clear error) or conditionally omitting dbname/defaulting it (e.g., to opts.Username) to preserve previous behavior for existing callers.

Suggested change
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s dbname=%s", opts.Host, opts.Username, opts.Password, opts.Port, opts.Database)
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port)
if opts.Database != "" {
dsn = fmt.Sprintf("%s dbname=%s", dsn, opts.Database)
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough review! I have applied both suggestions: made the dbname interpolation conditional and properly wired the logger for the Postgres configuration.

case POSTGRES:
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port)
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s dbname=%s", opts.Host, opts.Username, opts.Password, opts.Port, opts.Database)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQLITE branch applies opts.Logger to gorm.Config, but the POSTGRES branch always uses an empty gorm.Config{}. If callers set Options.Logger, it will currently be ignored for Postgres connections. Consider wiring opts.Logger.DatabaseLogger() into the Postgres gorm.Config the same way as SQLITE for consistent behavior.

Suggested change
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
config := &gorm.Config{}
if opts.Logger != nil {
config.Logger = opts.Logger.DatabaseLogger()
}
db, err := gorm.Open(postgres.Open(dsn), config)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough review! I have applied both suggestions: made the dbname interpolation conditional and properly wired the logger for the Postgres configuration.

Signed-off-by: Sahil Maurya <sahilmaurya5611@gmail.com>
@Omkar-Ugal
Copy link
Copy Markdown
Member

Can you clarify why dbname is needed here? What issue were we facing without it?

@sm-28601
Copy link
Copy Markdown
Author

sm-28601 commented Apr 7, 2026

Can you clarify why dbname is needed here? What issue were we facing without it?

Without the dbname parameter, PostgreSQL automatically tries to connect to a database matching the operating username. We needed this fix because users were unable to connect to a specific, custom database (like mesherydb) using the Options struct. It restricted the app to only using the default user-named database.

case POSTGRES:
dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if opts.Database != "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please validate and improve error handling when Database is empty for Postgres.

Is meshery set as the default database name somewhere?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please validate and improve error handling when Database is empty for Postgres.

Is meshery set as the default database name somewhere?

No, "meshery" is not set as a default database name anywhere. The Postgres path in meshkit is currently unused by Meshery (which uses SQLite) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(database): Add missing dbname parameter to PostgreSQL DSN connection string

4 participants