Create Database

Creates a new database in PostgreSQL.

Synopsis

CREATE DATABASE name
  [ WITH [ LOCATION = { 'dbpath' | DEFAULT } ]
         [ TEMPLATE = template | DEFAULT ]
         [ ENCODING = encoding_name | encoding_number | DEFAULT ] ]

Parameters

name

The name of the database you are creating.

dbpath

The directory in which to save the database. You may use the DEFAULT keyword to save the database in the default data directory, as specified by the PGDATA environment variable (or -D flag, passed to the postmaster).

Note that the dbpath value must normally be the name of system-level environment variable, which is set within the PostgreSQL user’s environment to a value describing an initialized database directory. However, if PostgreSQL was compiled by the gmake command with the CPPFLAGS=-DALLOW_ABSOLUTE_DBPATHS argument passed to it, you can also specify a complete, absolute directory path.

template

The name of the template you wish to base the new database off of. Use the DEFAULT keyword to specify the default template (usually template1).

encoding_name | encoding_num

The multibyte encoding method to use for the database. This can be entered as a string literal, or an encoding type’s corresponding integer encoding number. See Appendix A, for a list of PostgreSQL’s multibyte encoding types.

You may use the DEFAULT keyword to explicitly specify the default encoding method (this is already the default).

Results

CREATE DATABASE

The message returned when a new database is created successfully.

ERROR: user 'username ...

Get Practical PostgreSQL now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.