PGCLI: Python package for a interactive Postgres CLI
Last Updated :
28 Mar, 2022
PGCLI is a Python package that is used as an interactive command-line interface for the PostgreSQL database server. For following with the article, make sure you have PostgreSQL installed and set up in your local system. You can follow the guide for Windows, Linux and macOS from the guide.
Also for performing any operations or testing out the package, create a simple database of your choice, There should be enough data for you to work with as it is just a REPL for running queries in a Postgres database.
Installation
As pgcli is a python package, it can be installed with pip.
pip install pgcli
If you want to set up the package in an isolated environment, you can use the virtualenv. Following are the steps to install and setup pgcli in a virtual environment in python
pip install virtualenv
virtualenv venv
Windows:
venv\Scripts\activate
Linux/macos:
source venv/bin/activate
pip install pgcli
To make sure, you have installed pgcli in your system, you can execute the following command:
pgcli --version
After executing the command, the output should give out a particular/latest version of the package.
$ pgcli --version
Version: 3.4.1
Connecting to a database
Now, you can select a particular database that you want to work with. It should at least contain some tables(at least one) to work with. As we will perform reading records and other operations from pgcli.
pgcli <local_database_name> -U postgres
Here Postgres can be your user name(-U) which by default is postgres and the password is the master password for your postgres server on your system. The local database needs to be there in the postgres databases. I have selected the EMNS database in my local postgres server.
You also have multiple options to select and use in it like the
Get a List of all the databases for a user
We can obtain the list of all the databases in the postgres for a specific user using the list option.
pgcli --list -u postgres
As we can the list of all the databases on the postgres server with the Postgres user.
Running Queries in the repl
We can perform any queries as you would normally perform on the query tool in the pgadmin panel. If you want a simple guide on getting started with Postgres commands and queries, you can check this article.
For example, let us run a simple SELECT query in a particular database.
So, as we can we were able to execute SQL queries in the python package which is a CLI for interacting with the postgres server as a query tool.
Remote Database
We can even connect to a remote postgres database using some parameters or the URL itself.
pgcli 'postgresql://user:password@nlocalhost:port/db_name'
OR
pgcli -h localhost -U user -W '@postgres' -d db_name
We can use either of the commands to connect to a remote database in pgcli. The first command is using a single string that contains all the config data about the database and the host in a URL format. Whereas in the second command, we have to pass separate data in the form of parameters.
For further options and commands in pgcli, you can get help from the --help command:
pgcli --help
This will result in a list of commands and options which are available in the pgcli package to be used from the command line.
Thus, we can get more options like no-password or row limit for displaying only a few records in a query and so on.
So, pgcli is a command-line tool that can be used very similarly to the query tool in the pgAdmin provided by Postgres server.
Similar Reads
Perform PostgreSQL CRUD operations from Python
The DDL is comprised of the Create, Read, Update, Delete (CRUD) operations which form the backbone of any SQL database system. Let us discuss how to perform CRUD operations on a PostgreSQL database using python. Â Pyscopg2 is the most preferred mode and is widely used to connect the PostgreSQL databa
8 min read
How to install pyscopg2 package in Python?
psycopg2 is the most popular PostgreSQL database adapter for the Python programming language. It is a DB API 2.0 compliant PostgreSQL driver is actively developed. It is designed for heavily multi-threaded applications that create and destroy lots of cursors and create a large number of "INSERTs" or
1 min read
Packaging and Publishing Python code
If you have been coding in python, even for a while, you must be familiar with the concept of 'pip' by now. It is a package management system used to install and manage software packages/libraries written in Python. Then one may ask that where are all these packages/libraries stored? It is obvious t
7 min read
Python - Import CSV into PostgreSQL
In this article, we will see how to import CSV files into PostgreSQL using the Python package psycopg2. First, we import the psycopg2 package and establish a connection to a PostgreSQL database using the pyscopg2.connect() method. before importing a CSV file we need to create a table. In the example
2 min read
Save a image file on a Postgres database - Python
In this article, we are going to see how to save image files on a postgresql database using Python. Psycopg2 is a driver, that is used, for interacting, with Postgres data, using the Python scripting language. It is, used to perform, CRUD operations on Postgres data. Data handled in applications c
4 min read
Command Line Interface Programming in Python
This article discusses how you can create a CLI for your python programs using an example in which we make a basic "text file manager". Let us discuss some basics first. What is a Command Line Interface(CLI)? A command-line interface or command language interpreter (CLI), also known as command-line
7 min read
Insert Python list into PostgreSQL database
In this article, we will discuss how to Insert a Python list into PostgreSQL database using pyscopg2 module. Psycopg2 is the most popular PostgreSQL adapter for the Python programming language. Psycopg2 is a DB API 2.0 compliant PostgreSQL driver that is actively developed. It is designed for multi-
2 min read
PostgreSQL - Create Tables in Python
Creating tables in PostgreSQL using Python is an essential skill for developers working with databases. This article will explore the process of creating new tables in the PostgreSQL database using Python.Why Create PostgreSQL Tables with Python?Using Python to create PostgreSQL tables is beneficial
4 min read
Python | Database management in PostgreSQL
PostgreSQL is an open source object-relational database management system. It is well known for its reliability, robustness, and performance. PostgreSQL has a variety of libraries of API (Application programmable interface) that are available for a variety of popular programming languages such as Py
6 min read
PostgreSQL - Connect To PostgreSQL Database Server in Python
The psycopg database adapter is used to connect with PostgreSQL database server through python. Installing psycopg: First, use the following command line from the terminal: pip install psycopg If you have downloaded the source package into your computer, you can use the setup.py as follows: python s
4 min read