createdb
A core utility for creating a database
createdb
is a core utility for creating a database and is essentially a wrapper around the CREATE DATABASE
SQL command.
createdb
was added in PostgreSQL 7.0.
Usage
As of PostgreSQL 14, createdb
does not accept the -d option for providing a conninfo string; connection parameters must be provided individually or via environment variables
If the default postgres database does not exist, provide the name of a database to connect to with --maintenance-db
.
Since PostgreSQL 8.3, createdb
no longer emits any output on successful creation of a database.
Source code
The createdb
source code is located at src/bin/scripts/createdb.c.
Change history
- PostgreSQL 15
- PostgreSQL 9.2
--maintenance-db
option added (commit 68281e00)
- PostgreSQL 8.4
-w
/--no-password
option added (commit 9de59fd1)
- PostgreSQL 8.3
--quiet
option removed (commit 9539e64b)
- PostgreSQL 7.4
- converted from a shell script into a C program (commit 9e0ab712)
- PostgreSQL 7.3
- support for identifiers containing spaces added (commit 5804a7ce)
- PostgreSQL 7.1
--template
option added (commit c3b00e7e)
- PostgreSQL 7.0
- added (commit 240e4c98)
Examples
Basic usage example for createdb
:
$ createdb -h localhost -p 5432 -U postgres testdb $
Creating a database from template0:
$ createdb -h localhost -p 5432 -U postgres --template=template0 testdb
Attempting to create a database which already exists:
$ createdb -h localhost -p 5432 -U postgres postgres createdb: error: database creation failed: ERROR: database "postgres" already exists
References
- PostgreSQL documentation: createdb