Create a new database

Medium risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Creates a new, empty PostgreSQL database owned by the current user, ready for schema and data.

postgresqlcreatedatabasesetup

Parameters

Name for the new database.

Commands

Check whether a database with this name already exists

psql -c "SELECT datname FROM pg_database WHERE datname = '<database_name>';"

Create the new database owned by the current user

createdb <database_name>

Verification

psql -c "\l" | grep -w <database_name>

The new database name appears in the listing.

Undo

Remove the database and all of its objects

dropdb <database_name>

Pitfalls

  • Requires CREATEDB privilege for the current user.
  • Creating a database with a name that already exists will fail; the inspection step catches this.
  • The new database inherits the template1 template; any objects in template1 will be copied.

Related