PostgreSQL Create Database

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

PostgreSQL Create Database

PostgreSQL Create Database using pgAdmin


Step 1: In the Object Tree, right click and select create a database to Postgres create database

Step 2: In the pop-up,

i. Enter Database Name


ii. Comment if any database – optional
iii. Click Save

Step 3: DB is created and shown in the Object tree.

Step 4: The right pane gives you the SQL used to create the Database.
The syntax to create database in PostgreSQL is
CREATE DATABASE db_name
OWNER = role_name
TEMPLATE = template
ENCODING = encoding
LC_COLLATE = collate
LC_CTYPE = ctype
TABLESPACE = tablespace_name
CONNECTION LIMIT = max_concurrent_connection

Option Description
db_name Use this option to specify the name of the new database that you want to create.
Although, you need to make sure that the Database must be unique because If
you attempt to create a new database with the same name as an existing
database, PostgreSQL will display an error.
Use this parameter to define the role name for the user who will own the new
role_name
database. Default is Postgres
You can specify database template name from which you want to create the new
Template
database.
This parameter allows specifying character set encoding for the new database.
Encoding
Default is UTF8
The collation parameter specifies the sort order of strings which affect the result
Collate
of the ORDER BY clause while using a SELECT statement.
Option Description
It specifies the character classification for the new database. It affects the
Ctype
categorization, e.g., digit, lower and upper.
Using this option you can specify the tablespace name for the new database. The
tablespace_name
default is the template database’s tablespace.
Use this option to specify the maximum concurrent connections to the new
max_concurrent_connection
database. The default is -1, i.e., unlimited.

Common Errors while using the createdb command

Error Description
Createdb command not found. This kind of error may occur when PostgreSQL is not installed
correctly. At that time, you need to run createdb command from
your PostgreSQL installation path.
Error Description
No such file in the server is running
This error occurs when PostgreSQL Server is not started properly, or it
locally and accepting connections on
was not started where the createdb command wants it to start.
Unix domain socket.
This error may occur if the PostgreSQL user account is created which
FATAL role “usr name” does not exist
are different from system user accounts.
Permission denied to create a database If the PostgreSQL account is created does not have permission to
create a database In this case, you need to grant permission to the
associated users to access create command.

Summary

• You can create a database using the psql Windows Command Line (SQL Shell) with the command ” CREATE
DATABASE databasename
• You can also create a database using pgAdmin by following this step: In the Object Tree > right click on
Database > Select create a database
• One of the common errors encountered when create database command fails is that the server is not initialized
correctly. Rebooting the server solves the issue

You might also like