POSTGRESQL

CREATE DATABASE in PostgreSQL: A Complete Guide

intro

Let's learn everything you need to know about CREATE DATABASE in PostgreSQL, the statement you can employ to create new databases in your Postgres instance.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

Adding databases to a new database server is one of the most common tasks performed by DBAs or database users. While standard SQL does not provide a specific command for this, there are several options available, with the CREATE DATABASE command being the most common. Follow this guide to master CREATE DATABASE in PostgreSQL.

Let dive in!

What Is CREATE DATABASE in PostgreSQL?

In PostgreSQL, CREATE DATABASE is the statement used to create new databases. Keep in mind that—unlike standard SQL, where a database is typically a collection of schemas within a single catalog—PostgreSQL treats each database as a separate entity.

In particular, each Postgres database has its own catalog and operates independently. This means that schemas, tables, and other objects are confined to their respective databases, and you cannot directly access objects across different databases. Thus, cross-database queries are forbidden in PostgreSQL. To achieve similar functionality, you can use multiple schemas within the same database.

Note: CREATE DATABASE is not part of the SQL standard, which means database creation operations in SQL are implementation-specific.

Postgres CREATE DATABASE Syntax and Usage

This is the basic syntax to use CREATE DATABASE in PostgreSQL:

Copy
        
1 CREATE DATABASE db_name;

Where db_name is the name of the new Postgres database you want to create.

A more complete syntax for the Postgres CREATE DATABASE command includes a few optional parameters:

Copy
        
1 CREATE DATABASE db_name 2 [ WITH ] [ OWNER [=] user_name ] 3 [ TEMPLATE [=] template ] 4 [ ENCODING [=] encoding ] 5 [ STRATEGY [=] strategy ] 6 [ LOCALE [=] locale ];

Where:

  • user_name: The role of the user who will own the new database. Use DEFAULT to assign ownership to the user executing the command.
  • template: The name of the template database to use for creating the new database. Use DEFAULT to use the default template (template1).
  • encoding: The character set encoding for the new database. You can specify a string constant (e.g., 'SQL_ASCII'), an integer encoding number, or DEFAULT to use the encoding of the template database.
  • strategy: The strategy for creating the new database. The default WAL_LOG strategy copies the database block by block, efficiently logging each block in the write-ahead log. The older FILE_COPY strategy copies the database at the filesystem level, with a small log entry for each tablespace.
  • locale: Sets the default collation and character classification in the new database, which affects string sorting (e.g., in queries with ORDER BY) and the ordering in text column indexes.

Remember that other parameters are available. Discover them all through the official documentation.

Notes:

  • To create a database, you must be a superuser or have the CREATEDB privilege.
  • CREATE DATABASE cannot be executed inside a transaction block, as it is a DDL (Data Definition Language) command.
  • By default, a new database is created by cloning the template1 system database but you can specify a different template using the TEMPLATE name option. For example, TEMPLATE template0 creates a clean database with only the standard PostgreSQL objects, excluding any user-defined additions from template1.
  • Errors like “could not initialize database directory” are often due to insufficient permissions on the data directory, a full disk, or other file system issues.
  • Database-level configuration parameters (set via ALTER DATABASE) and permissions (set via GRANT) are not copied from the template database.
  • PostgreSQL does not provide a syntax for creating a database only if it does not already exist (like MySQL's CREATE DATABASE IF NOT EXISTS). In other words, PostgreSQL does not support the CREATE DATABASE IF NOT EXISTS syntax.

How to Create a Database in PostgreSQL

In the following sections, you will learn how to create a database in PostgreSQL using the CREATE DATABASE command or a visual database client.

With CREATE DATABASE

Connect to your PostgreSQL server and run the following query to create a new database called company with the default options:

Copy
        
1 CREATE DATABASE company;

Similarly, you can create a database called ecommerce with a specific locale, encoding, and template using the following query:

Copy
        
1 CREATE DATABASE ecommerce 2 LOCALE 'sv_SE.iso885915' 3 ENCODING LATIN9 4 TEMPLATE template0;

If you are wondering how to see the output of these commands, follow our guide on how to list databases in PostgreSQL.

With a Visual Database Client

The procedure outlined above works well, but not having visual feedback on what you are doing can make everything more complex. To combat this, run a Postgres CREATE DATABASE query in a visual database client like DbVisualizer:

Executing the CREATE DATABASE query in DbVisualizer
Executing the CREATE DATABASE query in DbVisualizer

That makes everything easier because you can clearly see that the query was executed successfully and that a new database was added to the server.

But what if you want to create a database without manually running a query, with just with a few clicks? In DbVisualizer, that is possible too!

Download DbVisualizer for free, connect to your PostgreSQL server, and navigate to the “Databases” tab on the right:

Reaching the PostgreSQL server connection
Reaching the PostgreSQL server connection

Expand the dropdown, right-click on "Database," and select the “Create Database…” option:

Selecting the Create Database… option
Selecting the “Create Database” option

The following modal will appear:

The database creation modal in DbVisualizer
The database creation modal in DbVisualizer

Here, you can create a new database by simply filling out the form and clicking the "Execute" button:

Filling out the database creation modal
Filling out the database creation modal

As you can see, the generated query is also shown in a dedicated text area! After clicking "Execute," confirm the creation of the database, and it will appear immediately with a special icon in the database list:

Note the newly created ecommerce database
Note the newly created ecommerce database

Note:

This database creation feature is available only in DbVisualizer Pro, which offers many other powerful features. Grab a 21-day free trial today!

Conclusion

In this article, you learned how to use CREATE DATABASE in PostgreSQL by exploring its syntax and seeing it in action through a couple of examples.. While you can run that query directly from the command line using psql, database management becomes much easier with a powerful, feature-rich database client like DbVisualizer, which offers full support for PostgreSQL and many other databases.

FAQ

What is the Postgres psql create database command?

The Postgres psql create database command on Linux is:

Copy
        
1 sudo -u postgres psql -c 'CREATE DATABASE db_name;'

The psql command connects to PostgreSQL as the postgres user. Then, it uses the -c flag to runs the CREATE DATABASE command directly.

Then, grant all permissions to the database with:

Copy
        
1 sudo -u postgres psql -c 'GRANT ALL PRIVILEGES on DATABASE db_name to user_name;'

This assigns full permissions to user_name, allowing them to manage the database. Note that both commands require superuser privileges, hence sudo -u postgres .

How to create a PostgreSQL database from the bash?

To create a PostgreSQL database from the bash shell without using psql, run the createdb command on your Linux PostgreSQL server:

Copy
        
1 sudo -u postgres createdb db_name

This creates db_name with default settings as the postgres user.

What are the privileges required to launch CREATE DATABASE in PostgreSQL?

To run CREATE DATABASE in PostgreSQL, you must be a superuser or have the CREATEDB privilege. To grant the CREATEDB privilege, use:

Copy
        
1 ALTER ROLE user_name CREATEDB;

This allows user_name to create databases without full superuser rights.

How to create a database schema in PostgreSQL?

In PostgreSQL, schemas organize database objects within a database. To create a new schema, connect to the database and run the following CREATE SCHEMA query:

Copy
        
1 CREATE SCHEMA schema_name;

Why use a visual database client to create a database in Postgres?

Using a visual database client like DbVisualizer to create a database in Postgres offers several advantages. It provides an intuitive, user-friendly interface, making database creation and management much easier compared to using command-line tools. With DbVisualizer, you can visually see your queries, monitor execution, and access powerful features like drag-and-drop query creation and query optimization. Test everything it has to offer today!

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

How Often Should SQL Transaction Logs Be Backed Up?

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-04-28
title

A Beginner's Guide to Vector Search Using pgvector

author Lukas Vileikis tags POSTGRESQL Vectors 3 min 2025-04-24
title

TRUNCATE vs DELETE: SQL Comparison of the Two Statements

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 9 min 2025-04-23
title

How to Extract the Year from a Date in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-04-21
title

How To List Tables In Postgres: Complete Guide

author Leslie S. Gyamfi tags POSTGRESQL 6 min 2025-04-17
title

A Guide to the SQL Standard Deviation Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-04-15
title

SQL EXISTS: Syntax and Use Cases with Examples

author Leslie S. Gyamfi tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-04-14
title

SQL TRUNCATE TABLE: A Complete Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-03-26
title

A Complete Guide to the FULL OUTER JOIN SQL Operation

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-03-24
title

How to Use JOIN in a DELETE Query in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-03-20

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.