How to create database in PostgreSQL

Database is collection objects like tables, indexes, views, triggers, etc. Following article explains ‘How to create database in PostgreSQL’, Syntax ,Parameters and Examples. To create database in PostgreSQL, user must have CREATE Database permission or user must be superuser. By default, the new database will be created by cloning the PostgreSQL system database template1

Syntax:

CREATE DATABASE name
[ [ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ TABLESPACE [=] tablespace_name ]
[ CONNECTION LIMIT [=] connlimit ] ]

Parameters and their descption:

Parameter Description
name Name of the database
user_name The user name who wants to own the database
template Name of the template to create new database
tablespace_name Name of the tablespace
lc_collate Collation Order
lc_ctype Character Classification
encoding Character set Details
connlimit Number of connection allowed to the database

Create Database in PostgreSQL

1. Connect to the PostgreSQL

2. Open Query Editor

3. Run the following command to create the database name ‘k2schools’, under ownership of postgres. Paste the below code in your PostgreSQL editor and click Play button or press functional key F5 to create Database.

CREATE DATABASE k2schools
WITH OWNER = postgres
ENCODING = ‘UTF8’
TABLESPACE = pg_default
LC_COLLATE = ‘English_United States.1252’
LC_CTYPE = ‘English_United States.1252’
CONNECTION LIMIT = -1;

4. Then refresh in the pgAdmin tool.

We can drop database by using drop command.

Syntax:

DROP DATABASE database_name

Related Posts

CheapSexCams
Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools