Updated May 15, 2023
Introduction to Postgres Command-Line
The following article provides an outline for Postgres Command-Line. You can interact with the PostgreSQL database using two main types of interfaces: command-line interaction and graphical user interface interaction. Here we see about one such command-line terminal-based tool available in PostgreSQL for user-database interaction. Psql is the front-end tool that is based on the terminal. Hence, you can easily and effectively interact with your PostgreSQL database using the psql utility from your terminal itself.
Whenever you download PostgreSQL, psql comes built-in it. You don’t need to take any efforts if you have Postgres installed on your machine. Here we will see how we can use psql as a command-line utility for PostgreSQL database interaction and some of the most basic commands you should know while dealing with PostgreSQL while using the command line.
psql [options...] [databaseName [nameOfUser]]
You can open your terminal and login to your shell user, and then for opening the psql command prompt, you can type the command in the above format where options, databaseName, and nameOfUser are optional arguments.
- Options: When connecting to the psql database, you can use various options such as -a, -b, -c, -d, etc. to indicate whether you want to display the results, queries, or other relevant information.
- databaseName: This is the database name you wish to connect to while entering the psql command prompt. By default, it is not mentioned; you will be connected to the database named Postgres if available, which is the case most of the time or template1 when Postgres is not present in your database system.
- nameOfUser: You can log in to the psql prompt with a particular user by specifying that user’s name over here.
Usage and Internals
Other than typing queries and issuing them to Postgres, psql can also fire the commands specified in an external file and use the metaCommands that make the task easier and allow us to write shell-like commands that enable us to write scripts using psql.
Whenever you type a command on the psql prompt, internally, this command is issued to the Postgres server, which is then interpreted and fired, and results are sent back to the psql and displayed on the psql terminal. We will take a look at some of the basic and crucial commands that a beginner should know when using the psql command-line interface to interact with the PostgreSQL database.
Examples of Postgres Command-Line
Given below are the examples mentioned:
Let us enter o the command shell with the Postgres user using the following command.
sudo su – postgres
If you have set a root password, you will be prompted to enter it in the following manner.
Let us now enter the psql prompt using the command.
If you want to enter the default database with the user already logged in, as in the above case, Postgres is the username.
Here it will ask for the password because we have already set it. After entering the password, the command prompt seems to be somewhat like this.
So, now we are going to logged in to the psql terminal prompt, and our current database is Postgres.
View all databases.
The first command we will see is \l, that lists all the databases present in the current database server. \l command gives the following output.
The \l command retrieves information related to databases, such as name, owner, encoding, collation, ctype, and access privileges.
View all tables.
To list out all the tables in the current database, enter the \dt command. We refer to tables in RDBMS as relations, and the command’ \dt’ produces the following output.
If no tables are in your database, a message saying “No relations found” will be displayed. The schema, name, type, and owner of the table are displayed using the \dt command.
Describe the table.
To retrieve information about the structure, constraints, and triggers on the table, we can use the \d command. This process is also known as describing the table. You can create the table using create table command if it doesn’t exist and then try to describe it if none is present in your database. For now, we will describe the table educational_platforms using the following command.
To switch your database and connect to some other database, you can use \c or \connect command as follows.
You pass the name of the database you want to connect to as an argument to the ‘educba’ command.
So, you are now connected to the educba database.
To know the syntax of a particular command.
If you want to find out the syntax of a particular command, you can use the \h command.
For example, if you want to know the ALTER TABLE command syntax, you can use the following command.
\h ALTER TABLE;
You can enter the \q command to exit from the retrieved result.
List out all commands.
If you want to know about all the available commands in psql for your convenience, you can use \? Command.
That gives the command syntax and its description.
If you want to know the information about the version of PostgreSQL you are using, then you can fire the following command.
Firing last fired command again,
Suppose you want to retrieve the last command you fired and fire the same. You can easily retrieve and fire the same using the \g command.
We can use a psql command-line from-end terminal-based tool available to us easily and efficiently. Many metacommands help us to manipulate and interact with our database to perform operations easily. The psql command-line allows you to write scripts and execute commands stored in external files.
We hope that this EDUCBA information on “Postgres Command-Line” was beneficial to you. You can view EDUCBA’s recommended articles for more information.