PostgreSQL Creating a Database
PostgreSQL can create a database in the following three ways:
- 1. Create using the CREATE DATABASE SQL statement.
- 2. Create using the createdb command.
- 3. Create using the pgAdmin tool.
CREATE DATABASE to Create a Database
The CREATE DATABASE command needs to be executed in the PostgreSQL command window; the syntax is as follows:
CREATE DATABASE dbname;
For example, we create a database named runoobdb:
postgres=# CREATE DATABASE runoobdb;
Creating a Database with the createdb Command
createdb is a wrapper around the SQL command CREATE DATABASE.
The syntax of the createdb command is as follows:
createdb [option...] [dbname [description]]
Parameter Description:
dbname: The name of the database to create.
description: A description of the newly created database.
options: Optional parameters, which can be the following values:
| No. | 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 help information for the createdb command. |
| 7 |
-h host Specify the hostname of the server. |
| 8 |
-p port Specify the port on which the server listens, or the socket file. |
| 9 |
-U username The username for connecting to the database. |
| 10 |
-w Ignore entering a password. |
| 11 |
-W Force a password prompt when connecting. |
Next, open a command window, go to the PostgreSQL installation directory, and enter the bin directory; the createdb command is located in PostgreSQL installation directory/bin ; execute the command to create the database:
$ cd /Library/PostgreSQL/11/bin/
$ createdb -h localhost -p 5432 -U postgres runoobdb
password ******
In the above command we used the superuser postgres to log in to the PostgreSQL database at host localhost, port 5432, and created the runoobdb database.
Creating a Database with the pgAdmin Tool
The pgAdmin tool provides complete functionality for operating on databases:
