createdb Command Reference: Flags, Usage & CI Examples
createdb creates a new PostgreSQL database without writing SQL.
createdb is a command-line wrapper around CREATE DATABASE. It connects to the server and creates an empty database, optionally setting owner, encoding, locale, or template.
Common flags and usage
- -h <host> / -p <port>: server host and port
- -U <user>: connect as this role (needs CREATEDB)
- -O <owner>: database owner
- -E <encoding>: character encoding (e.g. UTF8)
- -T <template>: template database (template0/template1)
Example
shell
PGPASSWORD=${PGPASSWORD} createdb -h localhost -U postgres -E UTF8 app_test \
|| echo "app_test already exists"In CI
When a container is reused across jobs the database may already exist; guard with || true, or drop it first, to keep the step idempotent. "permission denied to create database" means the role lacks CREATEDB, so connect as a superuser like postgres.
Key takeaways
- createdb wraps CREATE DATABASE for shell scripts.
- Guard against "already exists" to stay idempotent across reruns.
- The connecting role needs the CREATEDB privilege.
Related guides
psql Command Reference: Flags, Usage & CI ExamplesReference for the psql PostgreSQL client: connection flags, -c and -f, PGPASSWORD, ON_ERROR_STOP, and a CI ex…
pg_isready Command Reference: Flags, Exit Codes & CI ExamplesReference for pg_isready: -h, -p, -t, -q, the 0/1/2/3 exit codes, and a CI wait loop that blocks until a Post…
pg_restore Command Reference: Flags, Usage & CI ExamplesReference for pg_restore: -d, -C, -j, --clean, --no-owner, --exit-on-error, and a CI example that rebuilds a…