How to Create Database in PostgreSQL

โšก Smart Summary

PostgreSQL Create Database operations let you build a new database using either the psql command-line shell or the pgAdmin graphical tool. The CREATE DATABASE statement also accepts options for the owner, template, encoding, collation, tablespace, and connection limit.

  • ๐Ÿ’ป Command Line: Open the SQL Shell (psql), connect to the server, and run CREATE DATABASE to make a new database.
  • ๐Ÿ–ฑ๏ธ pgAdmin GUI: Right-click Databases in the Object Tree, choose Create, name the database, and save it visually.
  • ๐Ÿ“ Basic Syntax: The minimal command is CREATE DATABASE databasename, which builds a database with default settings.
  • โš™๏ธ Full Options: OWNER, TEMPLATE, ENCODING, LC_COLLATE, LC_CTYPE, TABLESPACE, and CONNECTION LIMIT customize the new database.
  • ๐Ÿ”Œ Connect and Verify: Use \l to list databases and \c databasename to connect to the one you created.
  • โš ๏ธ Common Errors: Watch for createdb not found, server not running, missing role, and permission-denied errors.

How to Create Database in PostgreSQL

The syntax to create database in PostgreSQL is

CREATE DATABASE databasename

Let’s learn setting up PostgreSQL and how to create database in PostgreSQL command line and the GUI

PSQL Create Database Command Line (SQL Shell)

Step 1) Open the SQL Shell

PSQL Create Database Command Line

Step 2) Press enter five times to connect to the DB

PSQL Create Database Command Line

Step 3) Enter the command

CREATE DATABASE guru99;

PSQL Create Database Command Line

Step 4) Enter command \l to get a list of all databases

PSQL Create Database Command Line

Step 5) To connect to a Database use PostgreSQL database command

\c guru99

PSQL Create Database Command Line

Prompt changes to guru99 which signifies that we are connected to database guru99 and can perform operations like create table, trigger, execute SQL on it.

PostgreSQL Create Database using pgAdmin

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

PostgreSQL Create Database using pgAdmin

Step 2) In the pop-up,

  1. Enter Database Name
  2. Comment if any database – optional
  3. Click Save

PostgreSQL Create Database using pgAdmin

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

PostgreSQL Create Database using pgAdmin

Step 4) The right pane gives you the SQL used to create the Database.

PostgreSQL Create Database using pgAdmin

Complete syntax to create a database

Here is complete Syntax to create a DB in PostgreSQL

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.
role_name Use this parameter to define the the role name for the user who will own the new database. Default is postgres
Template You can specify database template name from which you want to creates the new database.
Encoding This parameter allows specifying character set encoding for the new database. Default is UTF8
Collate The collation parameter specifies the sort order of strings which affect the result of the ORDER BY clause while using a SELECT statement.
Ctype It specifies the character classification for the new database. It affects the categorization, e.g., digit, lower and upper.
tablespace_name Using this option you can specify the tablespace name for the new database. The default is the template database’s tablespace.
max_concurrent_connection Use this option to specify the maximum concurrent connections to the new 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.
No such file in the server is running locally and accepting connections on Unix domain socket. This error occurs when PostgreSQL Server is not started properly, or it was not started where the createdb command wants it to start.
FATAL role “usr name” does not exist This error may occur if the PostgreSQL user account is created which 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.

FAQs

PostgreSQL creates a database named postgres by default during installation. It serves as the default connection target for tools and superusers, and it is used as a workspace before you create your own databases.

Use the ALTER DATABASE command, for example ALTER DATABASE oldname RENAME TO newname. You must be connected to a different database and no other sessions can be using the database you are renaming.

A template database is a model that PostgreSQL copies when creating a new database. template1 is the default template, so any object added to it appears in every new database. template0 is a clean, unmodifiable copy.

AI can generate a complete CREATE DATABASE statement from a plain description, filling in owner, encoding, and collation options correctly. It also explains each clause, helping beginners avoid syntax and configuration mistakes.

Yes. AI can read errors such as ‘permission denied’ or ‘role does not exist’, explain the cause, and suggest the exact GRANT or role command needed to resolve the problem quickly.

Summarize this post with: