PostgreSQL Create Database

PostgreSQL can create a database in the following three ways:

  • 1. UseCREATE DATABASESQL statement to create it.
  • 2. Usecreatedbcommand to create it.
  • 3. UsepgAdmintool.

CREATE DATABASE Create Database

The CREATE DATABASE command needs to be executed in the PostgreSQL command window. The syntax format is as follows:

CREATE DATABASE dbname;

For example, we create a database named exampledb:

postgres=# CREATE DATABASE exampledb;

createdb Command Create Database

createdb is a wrapper for the SQL command CREATE DATABASE.

The createdb command syntax format is as follows:

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

Parameter description:

dbname: The name of the database to be created.

description: Description related to the newly created database.

options: Optional parameters, which can be the following values:

Serial number Option & Description
1

-D tablespace

Specify the default tablespace for 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 locale of the database.

5

-T template

Specify the template for creating this database.

6

--help

Display the help information for the createdb command.

7

-h host

Specify the host name of the server.

8

-p port

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

9

-U username

The username for connecting to the database.

10

-w

Ignore password input.

11

-W

Force password input when connecting.

Next, we open a command window, go to the PostgreSQL installation directory, and enter the bin directory. The createdb command is located inPostgreSQL installation directory/binUnder it, execute the command to create the database:

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

In the above command, we used the superuser postgres to log in to the PostgreSQL database with host address localhost and port number 5432, and created the exampledb database.

pgAdmin Tool Create Database

The pgAdmin tool provides a complete set of database operation functions:

Other extensions