Important POSTGRESQL commands you should know — The Linux GURUS

Shujat Husain
3 min readDec 28, 2019

--

PostgreSQL is one of the most widely used databases in the world & is also very easy to administer. In this tutorial, we will learn some important postgresql commands that every beginner should know.

In our previous tutorials, we have learned to install postgresql on CentOS/RHEL , & also learned to install Postgresql from the source. So if you need to install postgresql first, please refer to those tutorials.

Recommended Read: How to setup SSH login without password on Linux systems

PostgreSQL commands

1- Connect to the database

Connect from localhost,

Now to connect to the database, we will run the command ‘psql’,

Once into the database, we can use ‘\q’ to exit the database,

Connecting from Remote server,

# psql -h host_ip -U user_name -p port_number -d database_name

To create a role, first connect to database & then we will use command ‘createuser’,

postgres=# CREATE USER test;

Or we can also use the following,

postgres=# CREATE ROLE test;

To create a user with password,

$ CREATE USER test PASSWORD ‘enter password here’

3- Delete a role/user

To delete a role, we will use the DROP command,

postgres=# DROP ROLE test;

4- Show list of users (List of roles)

To create a new database, use the following,

postgres=# CREATE DATABASE thelinuxgurus;

To delete a created database, use

postgres=# DROP DATABASE thelinuxgurus;

7- Show list of databases

8- Change DB when you are connected to other DB

postgres=# \c thelinuxgurus;

Or we can also user the following command,

postgres-#\connect new_database

To create a table, connect to the desired database & create a table with the following command,

thelinuxgurus=> CREATE TABLE USERS (Serial_No int, First_Name varchar, Last_Name varchar);

Now insert some records into it,

thelinuxgurus=> INSERT INTO USERS VALUES (1, ‘Dan’, ‘Prince’);

To delete a table from the database, we will use,

thelinuxgurus=> DROP TABLE USERS:

12- Describe a table to show its structure

13- Adding a column to a table

Once a table has been created, it can be altered to add new columns to it using the following command,

thelinuxgurus=> ALTER TABLE USERS ADD date_of_birth date;

We can not only alter the columns of a table but can also update the records that are entered in rows,

thelinuxgurus=> UPDATE USERS SET date_of_birth = ‘03–04–1990’ WHERE Seriel_No = ‘1’;

thelinuxgurus=> SELECT * FROM USERS;

The last command was to verify the changes that were made.

To remove a column from a table, run

thelinuxgurus=> ALTER TABLE USERS DROP date_of_birth;

To delete a row, use the following example,

thelinuxgurus=> DELETE FROM USERS WHERE Seriel_No = ‘1’;

17- List of all functions

To check all the functions on the database, use

To edit a function, use the following command,

postgres=# \ef function_name

19- List of all schema in DB

To see all the schemas on the database, use

20- To check the current Installed version

postgres=# select version();

21- To display the command history

22- Execute psql commands using a file

So if we have a file (like a .sql file) & we need to execute the file into our database, then use the following command

postgres=# \i /path/file_name

23- To turn on query execution time

24) To list database views

These were some of the important PostgreSQL commands that everyone should know about to maintain & administer the PostgreSQL database. Please feel free to send in any question or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Linux TechLab is thankful for your continued support.

Originally published at https://thelinuxgurus.com on December 28, 2019.

--

--

Shujat Husain

A TechEenthusiat who likes all new technologies & Gaming. Most of all love to write about Cloud & DevOPs & have my own blog as well (https://linuxtechlab.com).