Skip to content

Commit 5be3254

Browse files
authored
Fix: Postgres fails to connect when using special characters (#7262)
* Update client.go * Update remote.go * Update client.go
1 parent 5b92b75 commit 5be3254

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

backend/utils/postgresql/client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func NewPostgresqlClient(conn client.DBInfo) (PostgresqlClient, error) {
3232
return client.NewLocal(connArgs, conn.Address, conn.Username, conn.Password, conn.Database), nil
3333
}
3434

35-
connArgs := fmt.Sprintf("postgres://%s:%s@%s:%d/?sslmode=disable", conn.Username, conn.Password, conn.Address, conn.Port)
35+
// Escape username and password to handle special characters
36+
escapedUsername := url.QueryEscape(username)
37+
escapedPassword := url.QueryEscape(password)
38+
39+
connArgs := fmt.Sprintf("postgres://%s:%s@%s:%d/?sslmode=disable", escapedUsername, escapedPassword, conn.Address, conn.Port)
3640
db, err := sql.Open("pgx", connArgs)
3741
if err != nil {
3842
return nil, err

backend/utils/postgresql/client/remote.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (r *Remote) Backup(info BackupInfo) error {
134134
}
135135
fileNameItem := info.TargetDir + "/" + strings.TrimSuffix(info.FileName, ".gz")
136136
backupCommand := exec.Command("bash", "-c",
137-
fmt.Sprintf("docker run --rm --net=host -i %s /bin/bash -c 'PGPASSWORD=%s pg_dump -h %s -p %d --no-owner -Fc -U %s %s' > %s",
137+
fmt.Sprintf("docker run --rm --net=host -i %s -e PGPASSWORD='%s' /bin/bash -c 'pg_dump -h %s -p %d --no-owner -Fc -U %s %s' > %s",
138138
imageTag, r.Password, r.Address, r.Port, r.User, info.Name, fileNameItem))
139139
_ = backupCommand.Run()
140140
b := make([]byte, 5)
@@ -177,7 +177,7 @@ func (r *Remote) Recover(info RecoverInfo) error {
177177
}()
178178
}
179179
recoverCommand := exec.Command("bash", "-c",
180-
fmt.Sprintf("docker run --rm --net=host -i %s /bin/bash -c 'PGPASSWORD=%s pg_restore -h %s -p %d --verbose --clean --no-privileges --no-owner -Fc -U %s -d %s --role=%s' < %s",
180+
fmt.Sprintf("docker run --rm --net=host -i %s -e PGPASSWORD='%s' /bin/bash -c 'pg_restore -h %s -p %d --verbose --clean --no-privileges --no-owner -Fc -U %s -d %s --role=%s' < %s",
181181
imageTag, r.Password, r.Address, r.Port, r.User, info.Name, info.Username, fileName))
182182
pipe, _ := recoverCommand.StdoutPipe()
183183
stderrPipe, _ := recoverCommand.StderrPipe()

0 commit comments

Comments
 (0)