PostgreSQL provides two ways to create a new database: The first is the SQL command that uses the CREATE database. The second is a command-line executable file that uses Createdb.
The first is to create a database by using the creation databases.
Syntax format:
The basic syntax for the CREATE database statement is as follows:
CREATE DATABASE dbname; where dbname is the name of the database to be created.
To illustrate:
Here is a simple example that will create TestDB in PostgreSQL mode:
Description: We enter CREATE DATABASE TestDB; The system also returns the Create database, which indicates that the system created the TestDB successfully, where \h represents a view of the database in the system.
The second type: Use the Createdb command:
The PostgreSQL command line executable Createdb is a wrapper for SQL command create database. The only difference between this command and the SQL command create database is that the former can be run directly on the command line, and it allows comments to be added to the database, all in one command.
Grammar:
The CREATEDB syntax is as follows:createdb [option...] [dbname [description]]
Parameters
The following table lists the parameters and their descriptions.
Parameter name |
Describe |
dbname |
The name of the database to create. |
Description |
Specifies the comment to associate with the newly created database. |
Options |
This createdb accepts command-line arguments. |
Options
The following table lists the command line parameters Createdb receive:
Options |
Describe |
-D tablespace |
Specifies the default table space for the database. |
-E |
Duplicate createdb generated and sent to the server command. |
-E encoding |
Specifies the character encoding scheme to use in this database. |
-L Locale |
Specifies the locale that is used in the database. |
-T Template |
Specifies the template database to establish this database. |
--help |
Displays command line arguments dropdb help and exits. |
-H Host |
Specifies the host name of the machine on which the server is running. |
-P Port |
The server that specifies the TCP port or local UNIX domain socket file name extension listens for connections. |
-u username |
The user name of the connection. |
-W |
Never issued a password hint. |
-W |
Force createdb the password hint before connecting to the database. |
Open a command prompt, and then go to the directory where PostgreSQL is installed. Go to the Bin directory and execute the following command to create a database.
Createdb-h localhost-p 5432-u postgress TestDB
Password ******
The above command prompts Postgres for the default PostgreSQL Admin user's password in order to provide the password and continue to create a new database.
---restore content ends---
How to create a PostgreSQL database