February 13, 2023
On this tutorial, uncover the PostgreSQL Consumer Functions, and study ten of probably the most helpful command-line instruments.
If you determined to work with PostgreSQL databases, you most likely used graphical consumer interfaces equivalent to PGAdmin, DBeaver, and even paid ones like DataGrip. These software program purposes present user-friendly interfaces for working extra successfully with databases, and so they undoubtedly make database administration a lot simpler for database directors. However there’s a sturdy set of instruments referred to as PostgreSQL Consumer Functions that may actually leverage your effectivity whereas working with PostgreSQL databases. The excellent news is that these worthwhile instruments are bundled and include the PostgreSQL set up package deal at no cost.
The PostgreSQL Consumer Functions convey a fast and highly effective set of command-line instruments to the desk to make interacting with the PostgreSQL servers throughout platforms, native or distant, extra thrilling. On this tutorial, we’re going to find the PostgreSQL Consumer Functions and learn to get probably the most out of them by discussing ten of probably the most helpful command-line instruments.
We assume you already know the basics of SQL, together with the fundamentals of working with database administration methods. Should you’re unfamiliar with these or wanting to brush up in your SQL expertise, you may prefer to strive our SQL Fundamentals Ability Path — Dataquest. We additionally assume you have got already put in PostgreSQL in your laptop; in the event you haven’t, please discuss with the official PostgreSQL web site to obtain and set up it from right here.
PostgreSQL Consumer Functions
As I discussed, this bundle accommodates totally different instruments that help database directors and information consultants in getting probably the most out of PostgreSQL databases hosted on a neighborhood server, throughout networked servers, or within the cloud platforms. These command-line utilities, obtainable for Home windows, MacOS, and Linux working methods, are designed to work with and handle database objects. There are specific device units for creating and eradicating databases, roles, and customers. There are additionally some worthwhile instruments for making and restoring backups of databases.
Nevertheless, amongst these utilities, the decide of the litter is the psql command-line device, which permits us to connect with databases, discover their contents, execute SQL queries, and output the outcome units in varied codecs. This utility is known as a sport changer for leveraging the capabilities of PostgreSQL.
The psql Consumer Utility
Let’s begin exploring the PostgreSQL Consumer Functions with the psql command-line utility and its totally different choices.
However earlier than beginning work with this utility, let’s examine the PostgreSQL model put in in your native laptop by operating the next command:
% psql –version
psql (PostgreSQL) 14.5 (Homebrew)
The PostgreSQL model put in on my MacBook is 14.5, which permits me to connect with any PostgreSQL server that’s operating model 14.5 or earlier.
1. Hook up with a Database
Step one to entering into the psql command-line device is to connect with a neighborhood or distant PostgreSQL server.
To connect with a PostgreSQL database, you need to use the command template:
psql –host HOSTNAME –port PORT –user USERNAME –dbname DATABASE_NAME
The HOSTNAME is a distant server title or its IP deal with. If the PostgreSQL server is operating in your native machine, you need to use localhost as an alternative of a server’s IP deal with.
You additionally must determine the communication port with –port. The default communication port is 5432. You could omit the –port PORT argument if the port quantity is 5432.The –dbname and –username decide the database title to connect with and the username to attach with, respectively.
The next psql command makes a connection to the mydb database with username postgres that resides on the identical laptop that you’re engaged on, localhost.
% psql –host localhost –port 5432 –dbname mydb –username postgres
Executing the command above opens up a connection to the PostgreSQL server operating in your native laptop, and the command immediate might be modified as follows:
mydb=#
Now, you’ll be able to run psql‘s meta-commands, which we are going to talk about quickly. In the meantime, let’s shut the connection to the PostgreSQL server by typing q and urgent Enter, which can return you to the working system command immediate.
mydb=# q
Meta Instructions
The psql utility affords an excellent function referred to as meta, or backslash, instructions. These instructions or directions are processed by the psql shopper utility immediately and never executed by the PostgreSQL server. The meta-commands present quite a lot of choices. We’ll study extra about them within the following sections.
One of many meta-commands is q, which we tried earlier to stop the psql surroundings.Additionally, to get a listing of all obtainable meta-commands, you’ll be able to sort ? and press Enter.
mydb=# ?
2. The l Meta-Command
The l meta-command lets you checklist all of the databases saved on the PostgreSQL server you might be related to. First, connect with the database server, after which run the l meta-command, as proven within the picture under. The command lists all of the obtainable databases together with all the main points.
3. The dt Meta-Command
We’ve related to the mydb database; now we’re keen on itemizing all the present tables on this database. To take action, you need to use the dt meta-command as follows:
As proven within the picture above, all of the database’s relations (tables) are listed with helpful particulars equivalent to schema and the proprietor of tables.
4. The c Meta-Command
Generally, we have to change the energetic database and swap to a different one. The c meta-command permits us to connect with a brand new database. Let’s strive it.
So, we’ve made chinook the energetic database. Now, we will write queries towards the database. Let’s see how we will write a easy question towards the actor desk within the chinook database.
5. The d Meta-Command
To disclose the main points of every relation (desk, view, and so on.), we will use the d relation-name meta-command.
For instance, d workers returns the desk’s columns and their information sorts together with extra data for every column, as proven under:
6. The dn Meta-Command
The dn meta-command permits us to checklist all of the schemas present in a PostgreSQL database. To see the output of this meta-command, let’s connect with the Adventureworks database and checklist all the present schemas utilizing the dn meta-command.
7. The df Meta-Command
The df meta-command returns all of the obtainable capabilities of the present database. First, connect with the Adventureworks database, sort df, and press Enter to see the way it works. It’s going to present all of the capabilities present within the Adventureworks database.
8. The dv Meta-Command
The dv meta-command is one other helpful psql‘s backslash-command that permits us to checklist all of the views within the present database.
For instance, we will use this meta-command after connecting to the database to indicate the obtainable views within the chinook database.
NOTE
The psql shopper utility has dozens of meta-commands, and discussing all of them is past this tutorial’s scope. Should you’re keen on studying extra about these meta-commands, please discuss with PostgreSQl’s documentation portal.
Backup and Restore PostgreSQL Databases
Thus far, we’ve centered on utilizing the psql shopper utility. However there are some extra utilities bundled with PostgreSQL that make life simpler for database directors and information engineers — particularly for these information consultants who work on database upkeep duties.
9. The pg_dump Consumer Utility
The pg_dump shopper utility generates a file with SQL instructions to make a backup of a database. If we run these SQL statements, we are going to recreate precisely the identical database with the identical content material.
The next assertion makes a textual content file containing all of the SQL statements required for recreating the mydb database.
% pg_dump mydb > dump_file.sql
10. Restoring Plain-Textual content Backups
Restoring a database from a plain-text file generated by the pg_dump shopper utility is easy.
A lot of the database consultants use the psql utility with the next choices to recreate a database that now we have a backup file of in plain-text format:
% createdb mydb_restored
% psql –host localhost –dbname mydb_restored –username postgres –file /Customers/mohammadmehdi/dump_file.sql
% psql –host localhost –port 5432 –dbname mydb_restored –username postgres
The three instructions above will create a brand new database referred to as mydb_restored, then the dump file might be restored into it, which can recreate all of the database’s objects that we already made a backup of it utilizing the pg_dump utility.
The final command connects to the mydb_restored database. So, we will checklist all the present relations on this database to make sure it’s precisely the precise copy of the mydb database.
Abstract
This tutorial explored the PostgreSQL Consumer Functions by specializing in the psql utility and its meta-commands, making a database backup in a plain-text format utilizing pg_dump, then restoring it into a brand new database with psql.
This tutorial is simply an introduction to a number of the potentialities of the PostgreSQL Consumer Functions. Now the doorways to discovering extra about these utilities are open to you, and the perfect reference is PostgreSQL Consumer Functions reference.
I hope that you’ve got discovered one thing new right this moment. Be happy to attach with me on LinkedIn or Twitter.

Concerning the writer
Mehdi Lotfinejad
Mehdi is a Senior Information Engineer and Staff Lead at ADA. He’s an expert coach who loves writing information analytics tutorials.