Skip to content

Honor --sql-host, --sql-port and --sql-db for SQL backup/restore (external PostgreSQL)#204

Open
edmharlaoag wants to merge 1 commit into
chef:mainfrom
edmharlaoag:fix/sql-host-port-autoconfig-2907
Open

Honor --sql-host, --sql-port and --sql-db for SQL backup/restore (external PostgreSQL)#204
edmharlaoag wants to merge 1 commit into
chef:mainfrom
edmharlaoag:fix/sql-host-port-autoconfig-2907

Conversation

@edmharlaoag

@edmharlaoag edmharlaoag commented Jun 3, 2026

Copy link
Copy Markdown

Problem

knife ec backup --with-user-sql does not honor the SQL connection flags
and silently targets the wrong PostgreSQL server and database:

  • On external-PostgreSQL deployments it always connects to localhost:5432
    and fails with PG::ConnectionBad: Connection refused.
  • The configured database name is ignored, so it connects to a database
    named after the SQL user — servers whose DB name differs from the user
    name can't be backed up at all (issue --sql-db does not take effect #181).

Root cause

Both problems live in EcKeyBase:

  1. Host/portload_config_from_file! autoconfigures sql_user/
    sql_password/sql_db from /etc/opscode/chef-server-running.json but
    never reads the PostgreSQL host or port, while --sql-host/
    --sql-port are declared with hardcoded localhost/5432 defaults that
    always populate the config — so the target can never be derived from the
    running config.

    Note: this is not a = vs ||= issue. sql_db already used ||=,
    and host/port were never assigned in the autoconfigure block; a ||=
    there would have been a no-op against the defaulted option.

  2. Database name#db builds the connection URI from host, port, user,
    password and query params, but never writes config[:sql_db] into the URI
    path, so the resolved database name is dropped and PostgreSQL falls back to
    a user-named database (--sql-db does not take effect #181).

Fix

  • Remove the masking :default => "localhost"/5432 from the --sql-host/
    --sql-port options (ec_base.rb, ec_key_base.rb).
  • Source sql_host/sql_port from private_chef.postgresql.vip/port in
    load_config_from_file!, using ||= so an explicit --sql-host/
    --sql-port still wins.
  • Write the database name into the connection URI path in #db
    (server_uri.path = "/#{config[:sql_db]}" if config[:sql_db]) — the one-line
    fix originally proposed by @ioSpark in Pass --sql-db to URI builder if given #182.
  • Fall back to localhost:5432 in #db when host/port are unset, preserving
    embedded-PostgreSQL behavior.

Backward compatibility

Embedded-PostgreSQL installs report a loopback vip, and the #db fallback
covers the Automate / no-running-config paths, so those deployments are
unaffected. Explicit CLI flags retain top precedence.

Tests

Adds specs for: host/port adoption from chef-server-running.json, explicit
--sql-host/--sql-port precedence, and the database name appearing in the
connection URI.

Fixes #181. Refs #182, chef/chef-server#2907.

@edmharlaoag edmharlaoag requested review from a team as code owners June 3, 2026 21:05
@edmharlaoag edmharlaoag force-pushed the fix/sql-host-port-autoconfig-2907 branch 2 times, most recently from b00c932 to d7cc5b7 Compare June 3, 2026 21:18
@edmharlaoag edmharlaoag changed the title Autoconfigure SQL host/port from chef-server-running.json (fix external PostgreSQL backups) Honor --sql-host, --sql-port and --sql-db for SQL backup/restore (external PostgreSQL) Jun 3, 2026
knife ec backup --with-user-sql ignored the configured PostgreSQL
endpoint and database: it always connected to localhost:5432 and to a
database named after the SQL user. External-PostgreSQL deployments
failed with PG::ConnectionBad, and servers whose database name differs
from the SQL user name could not be backed up at all.

Two distinct problems in EcKeyBase:

1. Host/port were never derived from chef-server-running.json, and the
   --sql-host/--sql-port options carried hardcoded localhost/5432
   defaults that masked any other value. Drop those defaults, source
   sql_host/sql_port from private_chef.postgresql.vip/port in
   load_config_from_file! (||=, so an explicit flag still wins), and
   fall back to localhost:5432 in #db when unset.

2. The database name was resolved (from --sql-db or autoconfigure) but
   never written to the connection URI, so it was silently ignored and
   PostgreSQL fell back to a user-named database. #db now sets the URI
   path from config[:sql_db]. This follows the one-line fix proposed by
   @ioSpark in chef#182.

Adds specs for running-config host/port adoption, CLI precedence, and
the database name appearing in the connection URI.

Refs chef#181, chef#182, chef/chef-server#2907

Signed-off-by: edmharlaoag <laoagedmhar@gmail.com>
@jaymzh jaymzh force-pushed the fix/sql-host-port-autoconfig-2907 branch from d7cc5b7 to 64d9ee5 Compare June 11, 2026 20:56
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.

--sql-db does not take effect

1 participant