PostgreSQL Database Creation
PostgreSQL database creation can be done using the following three methods:
- Use the
CREATE DATABASE
SQL statement to create a database.
- Use the
- Use the
createdb
command to create a database.
- Use the
- Use the
pgAdmin
tool.
- Use the
CREATE DATABASE to Create a Database
The CREATE DATABASE command needs to be executed in the PostgreSQL command window, with the following syntax:
CREATE DATABASE dbname;
For example, to create a database named tutorialprodb:
postgres=# CREATE DATABASE tutorialprodb;
createdb Command to Create a Database
createdb is a wrapper for the SQL command CREATE DATABASE.
The createdb command syntax is as follows:
createdb [option...] [dbname [description]]
Parameter Description:
dbname: The name of the database to be created.
description: A description related to the newly created database.
options: Optional parameters, which can be the following values:
No. | Option & Description |
---|---|
1 | -D tablespace Specifies the default tablespace for the database. |
2 | -e Sends the createdb command to the server. |
3 | -E encoding Specifies the encoding for the database. |
4 | -l locale Specifies the language environment for the database. |
5 | -T template Specifies the template for creating this database. |
6 | --help Displays help information for the createdb command. |
7 | -h host Specifies the server's hostname. |
8 | -p port Specifies the port the server listens to, or the socket file. |
9 | -U username The username to connect to the database. |
10 | -w Ignores password input. |
11 | -W Forces password input upon connection. |
Next, open a command window, navigate to the PostgreSQL installation directory, and then to the bin directory. The createdb command is located in PostgreSQL installation directory/bin. Execute the command to create a database:
$ cd /Library/PostgreSQL/11/bin/
$ createdb -h localhost -p 5432 -U postgres tutorialprodb
password ******
The above command logs in as the superuser postgres to the PostgreSQL database at the host address localhost, port 5432, and creates the tutorialprodb database.
pgAdmin Tool to Create a Database
The pgAdmin tool provides full functionality for database operations.