Skip to content

Commit 240c7c7

Browse files
committed
createdb: Fix quoting of --encoding, --lc-ctype and --lc-collate
The original coding failed to properly quote those arguments, leading to failures when using quotes in the values used. As the quoting can be encoding-sensitive, the connection to the backend needs to be taken before applying the correct quoting. Author: Michael Paquier Reviewed-by: Daniel Gustafsson Discussion: https://postgr.es/m/20200214041004.GB1998@paquier.xyz Backpatch-through: 9.5
1 parent 1de4a82 commit 240c7c7

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/bin/scripts/createdb.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ main(int argc, char *argv[])
177177
dbname = get_user_name_or_exit(progname);
178178
}
179179

180+
/* No point in trying to use postgres db when creating postgres db. */
181+
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
182+
maintenance_db = "template1";
183+
184+
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
185+
prompt_password, progname, echo);
186+
180187
initPQExpBuffer(&sql);
181188

182189
appendPQExpBuffer(&sql, "CREATE DATABASE %s",
@@ -187,23 +194,25 @@ main(int argc, char *argv[])
187194
if (tablespace)
188195
appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
189196
if (encoding)
190-
appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
197+
{
198+
appendPQExpBufferStr(&sql, " ENCODING ");
199+
appendStringLiteralConn(&sql, encoding, conn);
200+
}
191201
if (template)
192202
appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
193203
if (lc_collate)
194-
appendPQExpBuffer(&sql, " LC_COLLATE '%s'", lc_collate);
204+
{
205+
appendPQExpBufferStr(&sql, " LC_COLLATE ");
206+
appendStringLiteralConn(&sql, lc_collate, conn);
207+
}
195208
if (lc_ctype)
196-
appendPQExpBuffer(&sql, " LC_CTYPE '%s'", lc_ctype);
209+
{
210+
appendPQExpBufferStr(&sql, " LC_CTYPE ");
211+
appendStringLiteralConn(&sql, lc_ctype, conn);
212+
}
197213

198214
appendPQExpBufferChar(&sql, ';');
199215

200-
/* No point in trying to use postgres db when creating postgres db. */
201-
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
202-
maintenance_db = "template1";
203-
204-
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
205-
prompt_password, progname, echo);
206-
207216
if (echo)
208217
printf("%s\n", sql.data);
209218
result = PQexec(conn, sql.data);

0 commit comments

Comments
 (0)