createdb Command in Linux



The createdb command is a utility in Linux that is part of the PostgreSQL object-relational database system. It is used to create a new PostgreSQL database. The command is a wrapper around the SQL command CREATE DATABASE, and there is no effective difference between creating databases via this utility and via other methods for accessing the server.

If you're comfortable with the command line and working primarily with PostgreSQL, then the createdb command offers a convenient and efficient way to create databases.

Table of Contents

Here is a comprehensive guide to the options available with the createdb command −

Understanding createdb Command

The createdb command in Linux is a powerful tool for managing PostgreSQL databases. It provides a convenient way to create new databases from the command line. createdb is specifically designed to create new databases in PostgreSQL, a widely used open-source relational database management system. createdb is a wrapper script for the CREATE DATABASE SQL command. They both achieve the same functionality.

Understanding these options can help users and administrators effectively manage their PostgreSQL databases, ensuring that they are set up with the appropriate configurations for their specific needs. Whether you are setting up a new project or managing multiple databases for different users, the createdb command is a fundamental tool in the PostgreSQL suite.

Prerequisite: Install createdb Command

The createdb command doesn't come installed independently. It's part of the PostgreSQL package, a powerful open-source relational database management system. Here's how to install PostgreSQL and gain access to createdb in Linux −

The specific commands will vary slightly depending on your Linux distribution, but the general process involves adding the PostgreSQL repository and then installing the packages.

For Debian/Ubuntu

Add the PostgreSQL repository −

sudo apt-get install postgresql-apt

Install PostgreSQL server and client packages −

sudo apt install postgresql postgresql-contrib

For Red Hat/CentOS/Fedora

Enable the PostgreSQL repository (replace <version> with your desired version, e.g., 14) −

sudo yum install https://download.postgresql.org/repo/redhat/rhel-<version>.repo

Install PostgreSQL server and client packages −

sudo yum install postgresql postgresql-contrib

Once the installation is complete, you can verify it by checking the PostgreSQL version −

psql --version
install_createdb_command

This displays the installed PostgreSQL version information.

Note − After successful installation, you can use the createdb command to create new databases as explained previously. Remember, you might need administrative privileges (sudo) depending on your configuration.

How to Use createdb Command in Linux?

The createdb command in Linux is specifically used for creating new databases in PostgreSQL, a popular open-source relational database management system.

Basic Syntax of createdb Command

This is the most basic form of the command.

createdb <database_name>

It creates a new database with the specified <database_name>. The user who executes this command will become the owner of the new database by default.

Here's a breakdown of the command and its various options −

OptionsDescriptions
dbnameSpecifies the name of the database to be created. It must be unique within the PostgreSQL instance.
-h, --host=<host>Specifies the hostname or IP address of the server.
-p, --port=<port>Specifies the port number of the server.
-U, --username=<username>Specifies the username to connect to the PostgreSQL server. By default, it uses the current user's credentials.
-W, --no-passwordAvoids prompting for a password when connecting to the server (if passwordless login is not configured).
-D or --tablespace=tablespaceAssigns the default tablespace for the new database.
-e or --echoEchoes the commands that createdb sends to the server.
-E or --encoding=encodingSets the character encoding scheme for the new database.
-l or --locale=localeDefines the locale for the new database. This option is equivalent to setting --lc-collate, --lc-ctype, and --icu-locale to the same value.
--lc-collate=localeSpecifies the LC_COLLATE setting for the new database.
--lc-ctype=localeDetermines the LC_CTYPE setting for the new database.
--icu-locale=localeSets the ICU locale ID for the new database, if the ICU locale provider is selected.
--icu-rules=rulesAllows for additional collation rules for the default collation of the new database, supported only for ICU.
--locale-provider={libc|icu}Chooses the locale provider for the database's default collation.
-O or --owner=ownerSpecifies the database user who will own the new database.
-V or --versionPrints the version of createdb and exits.
-S or --strategy=strategyIndicates the database creation strategy.
-T or --template=templateUses a template database as a basis for the new database.
-? or --helpDisplays help about the command-line arguments and exits.

The options -D, -l, -E, -O, and -T correspond to the options of the underlying SQL command CREATE DATABASE. For more detailed information about these options, one can refer to the PostgreSQL documentation.

Examples of createdb Command in Linux

Here are some examples of the createdb command in Linux to showcase its various functionalities −

  • Creating a Basic Database
  • Specifying a Database Owner
  • Connecting to a Specific Database
  • Creating a Database with a Template
  • Combining Options

Creating a Basic Database

This is the most straightforward scenario. To create a new database named "my_database", use −

createdb my_database
Creating Basic Database

This creates a new database with the name "my_database" and the current user will be the owner.

Specifying a Database Owner

To assign ownership to a different user, the -O option allows you to specify a different owner for the database.

Use createdb -O <owner_name> <database_name>

Here, <owner_name> represents the username who will own the new database. However, the user running the command needs to have sufficient privileges to assign ownership to another user.

Let's say you want to create a database named "accounting" and assign ownership to the user "postgres". You can achieve this with −

createdb -O postgres accounting
Specifying a Database Owner

In this case, the "accounting" database gets created, and the user "finance" becomes its owner.

Connecting to a Specific Database

This syntax is useful when you need to connect to a remote PostgreSQL server or a specific database on the local machine before creating a new one.

createdb -h <host> -p <port> -d <database_to_connect> <new_database_name>
  • <host> − The hostname or IP address of the remote server.
  • <port> − The port number on which the PostgreSQL server is listening (default is 5432).
  • <database_to_connect> − The name of the existing database to connect to on the server.
  • <new_database_name> − The name for the new database you want to create.

Imagine you need to create a database "sales" on a remote PostgreSQL server with hostname "dbserver" and port number 5433. You're also already connected to a local database named "inventory". Here's the command −

createdb -h dbserver -p 5433 -d inventory sales
Connecting to Specific Database

This connects to the PostgreSQL server on "dbserver" using port 5433, authenticates with the credentials of the "inventory" database user, and then creates a new database named "sales" on the remote server.

Creating a Database with a Template

PostgreSQL allows the creation of databases based on existing templates. These templates contain pre-defined configurations or objects. To create a database named "production" based on the "template1" template, use −

createdb -T template1 production
Creating Database with Template

This creates the "production" database using the settings and objects from the "template1" template.

Combining Options

You can combine various options for specific scenarios. For example, to create a database named "logs" with ownership assigned to "admin" and connect to a remote server using default credentials, run −

createdb -O admin -h mypostgresserver logs
Combining Options createdb Command 1

This assumes the remote server hostname is "mypostgresserver" and uses the default port (5432) and credentials of the current user to connect.

Remember, using sudo might be necessary depending on your system configuration, especially when managing ownership or connecting to remote servers.

It's important to note that not every user has the authorization to create new databases −

createdb --help
Combining Options createdb Command 2

If you encounter permission issues, you may need to consult with your site administrator or review the PostgreSQL documentation for guidance on user roles and permissions.

Alternatives of createdb Command

There isn't a direct alternative to the createdb command in Linux, as it's specifically designed for creating PostgreSQL databases. However, there are alternative approaches to achieve similar database creation functionality −

Using the psql Client

PostgreSQL provides the psql command-line client that allows you to interact with the database server. You can use it to directly execute the CREATE DATABASE SQL statement −

psql -h <host> -p <port> -U <username> -d postgres -c "CREATE DATABASE my_database;"

Here, you're connecting to the PostgreSQL server with specific details (host, port, username) and the postgres database (often used for administrative tasks). The -c flag allows you to execute the provided SQL command (CREATE DATABASE my_database;).

Using Programming Languages

Many programming languages like Python, Java, or Node.js have libraries or modules for interacting with databases. These libraries typically offer functions to create databases as part of their functionalities. Refer to the specific library documentation for details on creating databases.

Web Interfaces

Some database management systems, including PostgreSQL, might offer web-based user interfaces for managing databases. These interfaces often provide options for creating new databases through a graphical interface.

Conclusion

The createdb command in Linux is a utility for creating a new PostgreSQL database. It's a wrapper around the SQL command CREATE DATABASE, and it allows users to create a database from the command line. When executed, the user who runs the command typically becomes the owner of the new database, although it's possible to specify a different owner with the -O option, provided the user has the necessary privileges.

The command offers various options, such as setting the encoding, locale, and tablespace for the database, and it can echo the commands it generates with the -e option. It's important to note that createdb should be run from the shell and not within the psql interface.