Skip to content

Commit 06140c2

Browse files
committed
Add CREATE DATABASE LOCALE option
This sets both LC_COLLATE and LC_CTYPE with one option. Similar behavior is already supported in initdb, CREATE COLLATION, and createdb. Reviewed-by: Fabien COELHO <coelho@cri.ensmp.fr> Discussion: https://www.postgresql.org/message-id/flat/d9d5043a-dc70-da8a-0166-1e218e6e34d4%402ndquadrant.com
1 parent 3cae75f commit 06140c2

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

doc/src/sgml/ref/create_database.sgml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
2525
[ [ WITH ] [ OWNER [=] <replaceable class="parameter">user_name</replaceable> ]
2626
[ TEMPLATE [=] <replaceable class="parameter">template</replaceable> ]
2727
[ ENCODING [=] <replaceable class="parameter">encoding</replaceable> ]
28+
[ LOCALE [=] <replaceable class="parameter">locale</replaceable> ]
2829
[ LC_COLLATE [=] <replaceable class="parameter">lc_collate</replaceable> ]
2930
[ LC_CTYPE [=] <replaceable class="parameter">lc_ctype</replaceable> ]
3031
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
@@ -111,6 +112,26 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
111112
</para>
112113
</listitem>
113114
</varlistentry>
115+
<varlistentry>
116+
<term><replaceable class="parameter">locale</replaceable></term>
117+
<listitem>
118+
<para>
119+
This is a shortcut for setting <symbol>LC_COLLATE</symbol>
120+
and <symbol>LC_CTYPE</symbol> at once. If you specify this,
121+
you cannot specify either of those parameters.
122+
</para>
123+
<tip>
124+
<para>
125+
The other locale settings <xref linkend="guc-lc-messages"/>, <xref
126+
linkend="guc-lc-monetary"/>, <xref linkend="guc-lc-numeric"/>, and
127+
<xref linkend="guc-lc-time"/> are not fixed per database and are not
128+
set by this command. If you want to make them the default for a
129+
specific database, you can use <literal>ALTER DATABASE
130+
... SET</literal>.
131+
</para>
132+
</tip>
133+
</listitem>
134+
</varlistentry>
114135
<varlistentry>
115136
<term><replaceable class="parameter">lc_collate</replaceable></term>
116137
<listitem>
@@ -287,7 +308,7 @@ CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
287308
To create a database <literal>music</literal> with a different locale:
288309
<programlisting>
289310
CREATE DATABASE music
290-
LC_COLLATE 'sv_SE.utf8' LC_CTYPE 'sv_SE.utf8'
311+
LOCALE 'sv_SE.utf8'
291312
TEMPLATE template0;
292313
</programlisting>
293314
In this example, the <literal>TEMPLATE template0</literal> clause is required if
@@ -300,7 +321,7 @@ CREATE DATABASE music
300321
different character set encoding:
301322
<programlisting>
302323
CREATE DATABASE music2
303-
LC_COLLATE 'sv_SE.iso885915' LC_CTYPE 'sv_SE.iso885915'
324+
LOCALE 'sv_SE.iso885915'
304325
ENCODING LATIN9
305326
TEMPLATE template0;
306327
</programlisting>

src/backend/commands/dbcommands.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
124124
DefElem *downer = NULL;
125125
DefElem *dtemplate = NULL;
126126
DefElem *dencoding = NULL;
127+
DefElem *dlocale = NULL;
127128
DefElem *dcollate = NULL;
128129
DefElem *dctype = NULL;
129130
DefElem *distemplate = NULL;
@@ -184,6 +185,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
184185
parser_errposition(pstate, defel->location)));
185186
dencoding = defel;
186187
}
188+
else if (strcmp(defel->defname, "locale") == 0)
189+
{
190+
if (dlocale)
191+
ereport(ERROR,
192+
(errcode(ERRCODE_SYNTAX_ERROR),
193+
errmsg("conflicting or redundant options"),
194+
parser_errposition(pstate, defel->location)));
195+
dlocale = defel;
196+
}
187197
else if (strcmp(defel->defname, "lc_collate") == 0)
188198
{
189199
if (dcollate)
@@ -244,6 +254,12 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
244254
parser_errposition(pstate, defel->location)));
245255
}
246256

257+
if (dlocale && (dcollate || dctype))
258+
ereport(ERROR,
259+
(errcode(ERRCODE_SYNTAX_ERROR),
260+
errmsg("conflicting or redundant options"),
261+
errdetail("LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE.")));
262+
247263
if (downer && downer->arg)
248264
dbowner = defGetString(downer);
249265
if (dtemplate && dtemplate->arg)
@@ -276,6 +292,11 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
276292
parser_errposition(pstate, dencoding->location)));
277293
}
278294
}
295+
if (dlocale && dlocale->arg)
296+
{
297+
dbcollate = defGetString(dlocale);
298+
dbctype = defGetString(dlocale);
299+
}
279300
if (dcollate && dcollate->arg)
280301
dbcollate = defGetString(dcollate);
281302
if (dctype && dctype->arg)

src/bin/pg_dump/pg_dump.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,15 +2812,23 @@ dumpDatabase(Archive *fout)
28122812
appendPQExpBufferStr(creaQry, " ENCODING = ");
28132813
appendStringLiteralAH(creaQry, encoding, fout);
28142814
}
2815-
if (strlen(collate) > 0)
2815+
if (strlen(collate) > 0 && strcmp(collate, ctype) == 0)
28162816
{
2817-
appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
2817+
appendPQExpBufferStr(creaQry, " LOCALE = ");
28182818
appendStringLiteralAH(creaQry, collate, fout);
28192819
}
2820-
if (strlen(ctype) > 0)
2820+
else
28212821
{
2822-
appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
2823-
appendStringLiteralAH(creaQry, ctype, fout);
2822+
if (strlen(collate) > 0)
2823+
{
2824+
appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
2825+
appendStringLiteralAH(creaQry, collate, fout);
2826+
}
2827+
if (strlen(ctype) > 0)
2828+
{
2829+
appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
2830+
appendStringLiteralAH(creaQry, ctype, fout);
2831+
}
28242832
}
28252833

28262834
/*

src/bin/pg_dump/t/002_pg_dump.pl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,15 @@
14071407
like => { pg_dumpall_dbprivs => 1, },
14081408
},
14091409

1410+
"CREATE DATABASE dump_test2 LOCALE = 'C'" => {
1411+
create_order => 47,
1412+
create_sql => "CREATE DATABASE dump_test2 LOCALE = 'C' TEMPLATE = template0;",
1413+
regexp => qr/^
1414+
\QCREATE DATABASE dump_test2 \E.*\QLOCALE = 'C';\E
1415+
/xm,
1416+
like => { pg_dumpall_dbprivs => 1, },
1417+
},
1418+
14101419
'CREATE EXTENSION ... plpgsql' => {
14111420
regexp => qr/^
14121421
\QCREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;\E

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy