PostgreSQL — Story of the World’s Most Loved Advances Open Source Database (Part 2)

PART 2: Create database and PSQL commands

Shritam Kumar Mund
3 min readJul 19, 2020

Alright! In the previous article (Part 1), we saw, why PostgreSQL is so important to learn. And we have pretty much set up our environment to go ahead with some queries.

In “PART 2”, I’ll show how you can create your own database and also reveal a few common psql commands that will help you to interact with PSQL terminal faster and more effectively.

So, let’s get started.

FIRST thing FIRST:

As I had promised that, I’ll not go with any GUI throughout this series. So, Let’s open our PSQL terminal.

To know all available psql commands, you can use the \? command.

If you simply type “\?” and hit Enter, you’ll see that there is a bunch of things that we’re going to cover in this series. But now we’ll focus on some of the most used PSQL commands.

Alright, without further delay Lets begin.

1. Create your database:

Do you remember, on (PART 1), we saw there are 3 default databases Postgres, template0, and then template1. So by default, these are generated for us but we can create our own database. To do so we need to use a command that creates a fresh database for us. Let’s create our own postgreSQL database.

CREATE DATABASE mydb;

You have to give it a name, so the name can be anything you want, but what comes after it, is a semicolon; . Never ever forget to add a semicolon after any queries.

Now, list all databases to see whether the new database “mydb” has been added to our database list or not. You can use \l command to list all your DB’s:

As you can see, you have successfully added your new postgres DB “mydb”into the database list.

2. Connect to your database:

Once your DB has been created. You can connect to the new database:

\c mydb

You can see the previous connection has been closed. And you are connected with the “mydb” database.

3. List available tables in your databases

In a relational database, a table is a collection of related data held in a table format within a database. It consists of columns and rows.

To list all tables in the current database, you use \dt command:

As you can see, PSQL does not find any relations. This is because we don’t have any table inside our “mydb” database that we just created.

Perfect! In the next post, we’ll see all of the major functions that are implemented in PostgreSQL.

There is still a lot more to cover and learn. So, don’t miss the next article if you want to find out exactly how to perform CRUD operation (create, read, update, and delete) in PostgreSQL.

Stay tuned for the next post, PART 3.

--

--