Basic PostgreSQL Commands

Pradeep R.R.
YavarTechWorks
Published in
3 min readJul 28, 2022

Hi Friends,In this blog You’ll get learn basic commands about postgreSQL,

PostgreSQL is open source,powerful,object relational database.If you don’t have a postgreSQL in your terminal,Follow a basic commands to install postgreSQL.

Step 1:ADD PostgreSQL REPOSITORY

To install the repository,you first need to add in your system

$sudo apt-get install wget ca-certificates
$wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Step 2: Update the package list

To install postgres latest package we use these commands,

$sudo apt-get update

Step 3: Install PostgreSQl

To install postgres and the contrib package we use the following command

$sudo apt-get install postgresql postgresql-contrib

Step 4: check the postgreSQL version we use the following command

$apt show postgresql

Connect to PostgreSQL in the terminal we use the following command

$sudo su - postgres

Now, open a postgres prompt using the following command,

$psql

Create database name

# create database test;

Display all database

# \l

Dangerous command -delete database

#drop database databasename;

Connect to database

# \c test

Create a table with constraints

# create table person(
(# id BIGSERIAL NOT NULL PRIMARY KEY,
(# first_name VARCHAR(50) NOT NULL,
(# last_name VARCHAR(50) NOT NULL,
(# gender VARCHAR(8) NOT NULL,
(# date_of_birth DATE NOT NULL,
(# email VARCHAR(150));

Insert a value in the table

insert into person(id,first_name,last_name,gender,date_of_birth,email)values(1,'Raja','sekar','Male',DATE'21-06-1998','raja@gmail.com');

To view the table

# select * from person;

Orderby

# select * from person order by id ASC;
# select * from person order by id DESC;

Distinct

# select first_name from person;

Limit

WHERE

Like

NOT LIKE

Rename a column name

Delete rows in a table

Drop table

Conclusion

I hope this post helps you to learn the basic postgresql commands for your needs.Thank you for reading friends.

Reference link:

https://www.youtube.com/watch?v=qw--VYLpxG4

--

--