MHSanaei/3x-ui · error

open postgres source: %w

Error message

open postgres source: %w

What it means

Thrown by the PostgreSQL-to-SQLite export path when gorm.Open(postgres.Open(srcDSN)) cannot establish the source connection. GORM defers real connection, so this usually surfaces the driver's dial error: unreachable host, auth failure, unknown database, or TLS mismatch. Nothing has been written to the destination file yet when this fires.

Source

Thrown at internal/database/migrate_data.go:170

// ExportPostgresToSQLite copies every row from the PostgreSQL database described
// by srcDSN into a fresh SQLite file at dstPath. It is the reverse of
// MigrateData and is used to hand a PostgreSQL-backed panel a portable .db file.
// dstPath is created/overwritten; the PostgreSQL source is left untouched.
func ExportPostgresToSQLite(srcDSN, dstPath string) error {
	if srcDSN == "" {
		return errors.New("source DSN is required")
	}
	if err := os.MkdirAll(path.Dir(dstPath), 0o755); err != nil {
		return err
	}
	// Start from an empty file so AutoMigrate creates the canonical schema.
	if err := os.Remove(dstPath); err != nil && !os.IsNotExist(err) {
		return err
	}

	src, err := gorm.Open(postgres.Open(srcDSN), &gorm.Config{Logger: logger.Discard})
	if err != nil {
		return fmt.Errorf("open postgres source: %w", err)
	}
	srcSQL, err := src.DB()
	if err != nil {
		return err
	}
	defer srcSQL.Close()

	// No WAL: keep all data in the main file so it is complete once closed.
	dst, err := gorm.Open(sqlite.Open(dstPath+"?_busy_timeout=10000"), &gorm.Config{Logger: logger.Discard})
	if err != nil {
		return fmt.Errorf("open sqlite destination: %w", err)
	}
	dstSQL, err := dst.DB()
	if err != nil {
		return err
	}
	defer dstSQL.Close()

View on GitHub (pinned to ad32144c42)

Solutions

  1. Test the exact DSN outside the panel: psql "$SRC_DSN" -c 'SELECT 1' — if psql fails, fix the DSN/host/credentials first.
  2. Check server reachability (port open, security groups, pg_hba.conf entry for your client IP).
  3. Match sslmode to the server (disable/require/verify-full) and URL-encode special characters in the password.
  4. Confirm the database exists and the user has CONNECT privilege on it.
Defensive patterns

Strategy: validation

Validate before calling

// verify the source DSN connects BEFORE starting any export
sqlDB, err := sql.Open("pgx", srcDSN)
if err != nil { return err }
defer sqlDB.Close()
if err := sqlDB.PingContext(ctx); err != nil {
    return fmt.Errorf("source DSN unreachable: %w", err)
}

Prevention

When it happens

Trigger: Passing a malformed or wrong-format srcDSN; Postgres not running / firewalled; wrong password or user; database name typo; sslmode unsupported by the server (e.g. sslmode=require against a TLS-less server); DNS resolution failure.

Common situations: Exporting a backup from a remote/cloud Postgres whose pg_hba.conf or security group blocks the host; copying a DSN with URL-encoded special characters that got double-encoded; using a read-replica DSN whose credentials differ.

Related errors


AI-assisted analysis of MHSanaei/3x-ui@ad32144c42 (2026-08-15). Data as JSON: /api/errors/8d9b91352df8c26b. Report an issue: GitHub.