Postgresql

Create database from command line in PostgreSQL

20 September 2026 · 7 min read

Create database from command line in PostgreSQL

Managing databases is a crucial task for any application, and PostgreSQL provides powerful tools for doing so efficiently. One of the most effective methods for database management, especially in automated environments or when dealing with multiple databases, is to create database from command line in PostgreSQL. This approach allows for scripting, automation, and remote management, making it indispensable for developers and database administrators alike. In this guide, we’ll explore the step-by-step process, best practices, and potential pitfalls when creating databases using the command line interface in PostgreSQL. Knowing how to effectively manage PostgreSQL databases via the command line offers flexibility and control that GUI tools sometimes lack.

Understanding PostgreSQL Command-Line Tools

PostgreSQL offers several command-line tools, but the most commonly used for database administration is psql, the PostgreSQL interactive terminal. This tool allows you to connect to a PostgreSQL server, execute SQL commands, and manage databases directly. Another essential utility is createdb, a simpler command specifically designed for creating new databases. Understanding the difference between these tools is crucial. psql is a general-purpose tool for interacting with the database server, while createdb is a dedicated command for, as the name suggests, creating databases. Both are essential parts of the PostgreSQL toolkit for database administrators.

Before you can create database from command line in PostgreSQL, ensure that PostgreSQL is properly installed and configured on your system. You’ll also need appropriate user privileges to create databases. Typically, the postgres user or a user with CREATEDB privilege can create new databases. Incorrect permissions are a common stumbling block, so verifying user roles is a critical first step. If you are using a managed PostgreSQL service like Amazon RDS or Google Cloud SQL, the process may vary slightly, but the underlying principles remain the same.

According to the PostgreSQL documentation, “The createdb program is a utility for creating new PostgreSQL databases. It is a wrapper around the SQL command CREATE DATABASE.” PostgreSQL Documentation. This highlights the direct relationship between the command-line utility and the underlying SQL command.

Step-by-Step Guide: Creating a Database

The most straightforward way to create database from command line in PostgreSQL is using the createdb command. Here’s a step-by-step guide:

  1. Open your terminal or command prompt.
  2. Ensure you are logged in as a user with sufficient privileges (e.g., the postgres user). You may need to use sudo su postgres on Linux-based systems.
  3. Type the following command: createdb your_database_name. Replace your_database_name with the desired name for your new database.
  4. If the command is successful, you will see no output. To verify the creation, you can connect to the PostgreSQL server using psql and list the databases.
  5. To specify the database owner, you can use the -O option: createdb -O database_owner your_database_name.

Alternatively, you can use the psql command to execute the CREATE DATABASE SQL command. First, connect to the PostgreSQL server using psql -U your_user -d postgres. Replace your_user with your PostgreSQL username. Then, execute the SQL command: CREATE DATABASE your_database_name;. Don’t forget the semicolon at the end of the SQL command. This method provides more flexibility, allowing you to specify additional options such as encoding and tablespace.

For instance, to create a database named ecommerce_db with the owner web_user and UTF8 encoding using psql, you would execute the following commands: psql -U your_user -d postgres followed by CREATE DATABASE ecommerce_db OWNER web_user ENCODING ‘UTF8’;. Remember to replace your_user with your actual username. This demonstrates the power and flexibility offered by using SQL commands directly.

Advanced Options and Customization

When you create database from command line in PostgreSQL, you have several options to customize the database creation process. These options allow you to control various aspects of the database, such as encoding, locale, and tablespace.

  • Encoding: Specifies the character encoding for the database. UTF8 is the recommended encoding for most modern applications.
  • Locale: Defines the locale settings for the database, which affect sorting and formatting.
  • Tablespace: Determines where the database files are stored on the file system.

To specify these options using the createdb command, you can use the -E (encoding), -l (locale), and -T (tablespace) options. For example, createdb -E UTF8 -l en_US.UTF-8 -T pg_default your_database_name creates a database with UTF8 encoding, the en_US.UTF-8 locale, and the default tablespace. These options are crucial for ensuring that your database is properly configured for your specific application requirements.

The following paragraph is optimized as a featured snippet: When creating a database, choosing the correct encoding is crucial. UTF-8 is the most widely compatible encoding and is recommended for handling a wide range of characters. To specify UTF-8 encoding when creating a database via the command line, use the -E UTF8 option with the createdb command. This ensures that your database can store and retrieve data accurately, regardless of the characters used.

Troubleshooting Common Issues

Even with a clear understanding of the process, you might encounter issues when you create database from command line in PostgreSQL. Here are some common problems and their solutions:

  • Permission denied: Ensure you are logged in as a user with CREATEDB privileges or the postgres user.
  • Database already exists: Choose a unique database name or drop the existing database before recreating it.
  • Connection errors: Verify that the PostgreSQL server is running and that you have the correct connection parameters.

Another common issue is incorrect syntax. Double-check your commands for typos and ensure you are using the correct options. If you are using psql, make sure to terminate your SQL commands with a semicolon. Consulting the PostgreSQL documentation official documentation can often provide solutions to specific error messages. Remember that error messages, while sometimes cryptic, are your best clue to diagnosing and resolving problems.

For example, if you receive an error message stating “FATAL: role “your_user” is not permitted to create databases,” you need to grant the CREATEDB privilege to your user. You can do this by connecting to the postgres database as a superuser and executing the command ALTER ROLE your_user CREATEDB;. These small troubleshooting steps can save significant time and frustration.

Infographic here
FAQ: Creating Databases in PostgreSQL via Command Line ------------------------------------------------------
How do I check if a database exists using the command line?
You can connect to the PostgreSQL server using psql and execute the SQL command \\l (backslash l). This will list all available databases.
Can I create a database with a specific tablespace using the command line?
Yes, you can use the -T option with the createdb command, followed by the tablespace name. For example: createdb -T my\_tablespace my\_database.
What is the default encoding for a PostgreSQL database?
The default encoding depends on the PostgreSQL version and the operating system. However, it is generally recommended to explicitly specify UTF8 encoding for maximum compatibility.
How do I drop a database using the command line?
You can use the dropdb command followed by the database name. For example: dropdb my\_database. Be extremely careful with this command, as it will permanently delete the database and its contents.
Learning to **create database from command line in PostgreSQL** empowers you with the flexibility and control needed for efficient database management. By understanding the tools, options, and potential pitfalls, you can streamline your database creation process and ensure that your databases are properly configured for your applications. Remember to always verify permissions, double-check syntax, and consult the documentation when encountering issues. [Take the time to master these techniques](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c), and you'll find yourself a more capable and confident database administrator. Want to explore more advanced PostgreSQL topics? Consider delving into database backups, replication, and performance tuning for even greater expertise. Check out the official PostgreSQL website [here](https://www.postgresql.org/) for detailed documentation.

Question & Answer :
I am trying to create a database from command line. My OS is centos and postgres version is 10.9.

sudo -u postgres psql createdb test Password for user test: 

Why is it prompting me for the password?

Change the user to postgres :

su - postgres 

Create User for Postgres (in the shell and NOT with psql)

$ createuser testuser 

Create Database (same)

$ createdb testdb 

Acces the postgres Shell

psql ( enter the password for postgressql) 

Provide the privileges to the postgres user

$ alter user testuser with encrypted password 'qwerty'; $ grant all privileges on database testdb to testuser;