createdb
createdb — create a new QHB database
Synopsis
createdb [connection-option...] [option...] [dbname [description]]
Description
createdb creates a new QHB database.
Normally, the database user who executes this command becomes the owner of the new database. However, a different owner can be specified via the -O option, if the executing user has appropriate privileges.
createdb is a wrapper around the SQL command [CREATE DATABASE]. There is no effective difference between creating databases via this utility and via other methods for accessing the server.
If the database name is not specified, user name of the user executing the program will be used; the same algorithm will be used for database owner.
If the database can't be created, createdb will print error message (which sometimes offering problem solution).
Options
createdb accepts the following command-line arguments:
dbname
Specifies the name of the database to be created. The name must be unique among
all QHB databases in this cluster. The default is to create a
database with the same name as the current system user.
description
Specifies a comment to be associated with the newly created database.
-D tablespace
--tablespace=tablespace
Specifies the default tablespace for the database. (This name is processed as a
double-quoted identifier.)
-e
--echo
Echo the commands that createdb generates and sends to the server.
-E encoding
--encoding=encoding
Specifies the character encoding scheme to be used in this database. The character
sets supported by the QHB server are described in Section
Supported Character Sets.
-l locale
--locale=locale
Specifies the locale to be used in this database. This is equivalent to
specifying both --lc-collate and --lc-ctype.
--lc-collate=locale
Specifies the LC_COLLATE setting to be used in this database.
--lc-ctype=locale
Specifies the LC_CTYPE setting to be used in this database.
-icu-locale=locale
Specifies the ICU locale ID to be used in this database, if the ICU locale
provider is selected.
--locale-provider={libc|icu}
Specifies the locale provider for the database's default collation.
-O owner
--owner=owner
Specifies the database user who will own the new database. (This name is
processed as a double-quoted identifier.)
-S template
--strategy=strategy
Specifies the database creation strategy. See [CREATE DATABASE] for more
details.
-T template
--template=template
Specifies the template database from which to build this database. (This name is
processed as a double-quoted identifier.)
-v
--verbose
Specifies the logging verbosity in Debug. The default is Info.
-V
--version
Print the createdb version and exit.
-?
--help
Show help about createdb command line arguments, and exit.
The options -D, -l, -E, -O, and -T correspond to options of
the underlying SQL command [CREATE DATABASE]; see there for more information
about them.
createdb also accepts the following command-line arguments for connection parameters:
-h host
--host=host
Specifies the host name of the machine on which the server is running. If the
value begins with a slash, it is used as the directory for the Unix domain socket.
-p port
--port=port
Specifies the TCP port or local Unix domain socket file extension on which the
server is listening for connections.
-U username
--username=username
User name to connect as.
-w
--no-password
Never issue a password prompt. If the server requires password authentication
and a password is not available by other means such as a .pgpass file, the
connection attempt will fail. This option can be useful in batch jobs and scripts
where no user is present to enter a password.
-W
--password
Force createdb to prompt for a password before connecting to a database.
This option is never essential, since createdb will automatically prompt
for a password if the server demands password authentication. However,
createdb will waste a connection attempt finding out that the server wants
a password. In some cases it is worth typing -W to avoid the extra connection
attempt.
--maintenance-db=dbname
Specifies the name of the database to connect to when creating the new database.
If not specified, the qhb database will be used; if that
does not exist (or if it is the name of the new database being created),
template1 will be used. This can be a connection string. If so, connection
string parameters will override any conflicting command line options.
Environment
PGDATABASE
If set, the name of the database to create, unless overridden on the command line.
PGHOST
PGPORT
PGUSER
Default connection parameters. PGUSER also determines the name of the database
to create, if it is not specified on the command line or by PGDATABASE.
PG_COLOR
Specifies whether to use color in diagnostic messages. Possible values are
always, auto and never.
This utility, like most other QHB utilities, also uses the environment variables supported by libpq (see Section Environment Variables).
Diagnostics
In case of difficulty, see [CREATE DATABASE] and [psql] for discussions
of potential problems and error messages. The database server must be running at
the targeted host. Also, any default connection settings and environment variables
used by the libpq front-end library will apply.
Examples
Use Cases
To create the database new using the default database server:
$ createdb new
To create the database new using user name of the user executing the program:
$ createdb
To create the database new based upon the template0 database using the en_US.UTF-8 locale, and connecting to the qhb maintenance database:
$ createdb new -l en_US.UTF-8 -T template0 --maintenance-db qhb
To create the database new based upon the template0 database using the en_us locale and the LATIN1 encoding.
$ createdb new "my comment" -l en_US -E LATIN1 -T template0 -v
After creating, the "my comment" comment will be added to the database.
The -v switch provides debug information output:
[DEBUG] Running getpwuid_r for user #1000
[DEBUG] Loading user with uid 1000
[DEBUG] executing statement batch: SELECT pg_catalog.set_config('search_path', '', false);
[DEBUG] executing statement batch: CREATE DATABASE new2 ENCODING 'LATIN1' TEMPLATE template0 LC_COLLATE 'en_US' LC_CTYPE 'en_US';
[DEBUG] executing statement batch: COMMENT ON DATABASE new2 IS 'my comment';
[DEBUG] Database has been successfully created
Specifications
-
Entries
createdb new -l en_US.UTF-8 -T template0and
createdb new -l en_US -E UTF-8 -T template0are not equivalent. In the former case the database will be created successfully; in the latter case an error will occur.
[ERROR] db error: ERROR: encoding "UTF8" does not match locale "en_US": SQL: "CREATE DATABASE new3 ENCODING 'UTF-8' TEMPLATE template0 LC_COLLATE 'en_US' LC_CTYPE 'en_US';" DETAIL: The chosen LC_CTYPE setting requires encoding "LATIN1". -
You can't add a comment to the database without specifying the database name.
-
The -l/--locale switch is equivalent to specifying both --lc-collate and --lc-ctype, so they should not be specified simultaneously. That is:
createdb new -l en_US = CREATE DATABASE new LC_COLLATE 'en_US' LC_CTYPE 'en_US';When this two options are specified simultaneously
createdb new --lc-collate en_US --lc-ctype en_USan error will occur.
-
By default, createdb tries to use the template1 database as the maintenance database for the new database (--maintenance-db switch), and if createdb can not connect to template1, the qhb database will be used.
-
In addition to debug information, the -v/--verbose switch acts as ECHO and echoes the SQL commands that are sent to the server for execution.
See Also
dropdb, [CREATE DATABASE]