Skip to content
Latchkey

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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →