Hello! In this article we will talk about how to create a database in PostgreSQL in the Linux terminal.
Psql is a terminal client we can use to manipulate database objects.
For more information you can check Postgresql documentation for CREATE DATABASE statement.
How to Create Database in Postgresql in Linux Terminal?
First of all you need to craft your create database statement.
Since our purpose for this article is creating database i will not cover create database parameters.
Let’s assume we will create a database named MYDB. Our statement would be:
CREATE DATABASE MYDB;

Using psql
Run psql to enter PostgreSQL command line.

Simple issue the statement below to create a new database.

You can see MYDB exists after issuing \l command:

Let’s drop it and recreate it using another method.
Using echo – Bash Scripts
This method is more appropriate for bash scripts. Also you can run any psql commands using this method.
echo "CREATE DATABASE MYDB;" | psql

As you see, echo can be used to pass commands to psql.
When you run Psql commands using this method you can use $? variable to check it’s return codes.
psql returns 0 to the shell if it finished normally, 1 if a fatal error of its own occurs (e.g., out of memory, file not found), 2 if the connection to the server went bad and the session was not interactive, and 3 if an error occurred in a script and the variable
https://www.postgresql.org/docs/current/app-psql.html#:~:text=psql%20returns%200%20to%20the,the%20variable%20ON_ERROR_STOP%20was%20set.ON_ERROR_STOPwas set.
You may want to check my article about connecting to a Postgresql database.
To Sum Up
In this article, we explored the process of creating a database in PostgreSQL using the Linux terminal. We began by discussing the use of the psql terminal client for database manipulation, and provided a simple example of crafting a database creation statement. The article also demonstrated the use of psql and the \l command to confirm the existence of the newly created database. Additionally, an alternative method using echo and Bash scripts to run psql commands was illustrated. The use of the $? variable to check return codes when running psql commands via echo was also highlighted. Readers are encouraged to provide their feedback on whether this tutorial effectively explained the creation of a PostgreSQL database in the Linux terminal, and if there are any additional topics they would like to see covered in future articles.
We value your opinion! Did this tutorial effectively guide you through the process of creating a PostgreSQL database in the Linux terminal? Please share your thoughts and feedback with us. Additionally, if there are specific topics or techniques related to PostgreSQL or Linux terminal operations that you would like us to explore further, we would love to hear your suggestions. Your input is essential in helping us provide valuable and relevant content to our readers.
Hi! I’m an IT Specialist
I want to hear from you! I am Working with enterprises for 10+ years to improve their infrastructure and efficiency.
Get in touch with me.




Leave a Reply