English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Create a Database in PostgreSQL

PostgreSQL can create a database in the following three ways:

  • 1Using the CREATE DATABASE SQL statement to create.

  • 2Using the createdb command to create.

  • 3Using pgAdmin tool.

CREATE DATABASE creates a database

The CREATE DATABASE command must be executed in the PostgreSQL command window, with the syntax format as follows:

CREATE DATABASE dbname;

For example, we create a w3codeboxdb database:

postgres=# CREATE DATABASE w3codeboxdb;

The createdb command creates a database

createdb is a wrapper for the SQL command CREATE DATABASE.

The syntax format of the createdb command is as follows:

createdb [option...] [dbname [description]]

Parameter Description:

dbnameDescription: The name of the database to be created.

descriptionDescription: Information related to the newly created database.

optionsParameters available can be the following values:

Serial NumberOptions & Descriptions
1

-D tablespace

Specify the default tablespace of the database.

2

-e

Send the command generated by createdb to the server.

3

-E encoding

Specify the encoding of the database.

4

-l locale

Specify the language environment of the database.

5

-T template

Specify the template to create this database.

6

--help

Displays the help information of the createdb command.

7

-h host

Specify the hostname of the server.

8

-p port

Specify the port that the server listens on, or the socket file.

9

-U username

The username of the user connecting to the database.

10

-w

Ignores the input of a password.

11

-W

Forces the input of a password during the connection.

Next, we open a command window, enter the PostgreSQL installation directory, and enter the bin directory, where the createdb command is located PostgreSQL installation directory/bin Below, execute the command to create a database:

$ cd /Library/PostgreSQL/11/bin/
$ createdb -h localhost -p 5432 -U postgres w3codeboxdb
password ******

The above command uses the superuser postgres to log in to the host address of localhost, port number of 5432 and create w in the PostgreSQL database3codeboxdb database.

pgAdmin tool creates a database

pgAdmin tool provides complete database operation functions: