createdb
Create a new PostgreSQL database
TLDR
Create a database owned by the current user
Create a database owned by a specific user with a description
Create a database from a template
SYNOPSIS
createdb [options] [dbname [description]]
PARAMETERS
-D tablespace
or --tablespace=tablespace
Specifies the default tablespace for the database. If not specified, the database will use the default tablespace of the template database. -E encoding
or --encoding=encoding
Specifies the character set encoding to use for the new database. For example, UTF8
, LATIN1
. -l locale
or --locale=locale
Specifies the locale to use for the new database. -O dbowner
or --owner=dbowner
Specifies the user account that will own the new database. By default, this is the user running createdb
. -T template
or --template=template
Specifies the template database from which to create the new database. The default is template1
. -U username
or --username=username
Connect to the database server as the given user. -w
or --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. -W
or --password
Force createdb
to prompt for a password before connecting to the database. -e
or --echo
Show the SQL commands that createdb
generates. -h hostname
or --host=hostname
Specifies the hostname of the database server to connect to. -p port
or --port=port
Specifies the port number of the database server to connect to. -V
or --version
Print the createdb
version and exit. --help
Show help about createdb
command line arguments, and exit. dbname
The name of the database to create.
DESCRIPTION
The createdb
command is a utility for creating new PostgreSQL databases. It's a wrapper around the SQL command CREATE DATABASE
. It allows you to create databases from the command line with various options for specifying the database name, owner, encoding, template, and other properties.
The command simplifies the process of database creation for administrators and users who may not be familiar with SQL directly. It handles the underlying SQL commands and connection details, allowing users to focus on defining the characteristics of the database.
It connects to the PostgreSQL server using the connection parameters specified on the command line or in the environment variables. When executed successfully, it creates a new, empty database ready for use. If an error occurs (e.g., database already exists, insufficient permissions), createdb
will display an error message. It is typically used in scripts or by system administrators to automate database provisioning.
CAVEATS
Requires appropriate permissions to create databases on the PostgreSQL server. The user running createdb
must either be a superuser or have CREATEDB
privilege. If no template is specified, `template1` is used, which must be a suitable clean template database.
ENVIRONMENT VARIABLES
createdb
uses the environment variables PGHOST
, PGPORT
, PGUSER
, and PGDATABASE
for connection parameters. It also uses PGPASSWORD
(though it's recommended to use .pgpass
instead for security). These are the same variables used by other PostgreSQL client applications.
EXIT STATUS
createdb
exits with status 0 if successful, and with status 1 if an error occurs.