Wednesday, 15 May 2019

PostgreSQL installation on CentOS 7 and basic commands

PostgreSQL is a powerful, open source object-relational database system with  a strong reputation for reliability,
feature robustness, and performance. Many applications uses PostgreSQL as their database.

Here , i will show the way to install postgreSQL db on your centos server.


My Hardware Specs, Hosted on VirtualBox


Linux Server : CentOS Linux release 7.5.1804
Memory    : 1GB


By default CentOS contains postgreSQL packages within its default repositories, but we will install it from its repo


Install the Postgres repo
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-ppc64le/pgdg-redhat-repo-latest.noarch.rpm


Now you can get the list from the repositories


yum list | grep postgres


For example  refer below ,


[root@mydbinfo ~]# yum list | grep postgresql
Failed to set locale, defaulting to C
calligra-kexi-driver-postgresql.x86_64   2.9.10-2.el7 epel
collectd-postgresql.x86_64               5.8.1-1.el7 epel
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
postgresql94.x86_64                      9.4.22-1PGDG.rhel7 pgdg94
postgresql94-contrib.x86_64              9.4.22-1PGDG.rhel7 pgdg94
postgresql94-debuginfo.x86_64            9.4.22-1PGDG.rhel7 pgdg94
postgresql94-devel.x86_64                9.4.22-1PGDG.rhel7 pgdg94
postgresql94-docs.x86_64                 9.4.22-1PGDG.rhel7 pgdg94
postgresql94-jdbc.noarch                 9.4.1207-2.rhel7 pgdg94
postgresql94-jdbc-javadoc.noarch         9.4.1207-2.rhel7 pgdg94
postgresql94-libs.x86_64                 9.4.22-1PGDG.rhel7 pgdg94
postgresql94-odbc.x86_64                 11.00.0000-1PGDG.rhel7 pgdg94
postgresql94-odbc-debuginfo.x86_64       09.03.0400-1PGDG.rhel7 pgdg94
postgresql94-plperl.x86_64               9.4.22-1PGDG.rhel7 pgdg94
postgresql94-plpython.x86_64             9.4.22-1PGDG.rhel7 pgdg94
postgresql94-pltcl.x86_64                9.4.22-1PGDG.rhel7 pgdg94
postgresql94-server.x86_64               9.4.22-1PGDG.rhel7 pgdg94
postgresql94-tcl.x86_64                  2.4.0-1.rhel7 pgdg94
postgresql94-tcl-debuginfo.x86_64        2.3.1-1.rhel7 pgdg94
postgresql94-test.x86_64                 9.4.22-1PGDG.rhel7 pgdg94
postgresql95.x86_64                      9.5.17-1PGDG.rhel7 pgdg95
postgresql95-contrib.x86_64              9.5.17-1PGDG.rhel7 pgdg95
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
postgresql_anonymizer10.noarch           0.2.1-1.rhel7 pgdg10
postgresql_anonymizer11.noarch           0.2.1-1.rhel7 pgdg11
postgresql_anonymizer95.noarch           0.2.1-1.rhel7 pgdg95
postgresql_anonymizer96.noarch           0.2.1-1.rhel7 pgdg96
proftpd-postgresql.x86_64                1.3.5e-4.el7 epel
python-testing.postgresql.noarch         1.1.0-3.el7 epel
qt-postgresql.i686                       1:4.8.7-2.el7 base
qt-postgresql.x86_64                     1:4.8.7-2.el7 base
qt5-qtbase-postgresql.i686               5.9.2-3.el7 base
qt5-qtbase-postgresql.x86_64             5.9.2-3.el7 base
soci-postgresql.x86_64                   3.2.3-1.el7 epel
soci-postgresql-devel.x86_64             3.2.3-1.el7 epel
[root@mydbinfo ~]#


The above shows the list of software available from the repo, depending on your need you can install the needed version.


To be precise to now what all versions are available


[root@mydbinfo ~]# yum list | grep postgresql | grep server.x86_64
Failed to set locale, defaulting to C
postgresql-server.x86_64                 9.2.24-1.el7_5 base
postgresql10-server.x86_64               10.8-1PGDG.rhel7 pgdg10
postgresql11-server.x86_64               11.3-1PGDG.rhel7 pgdg11
postgresql94-server.x86_64               9.4.22-1PGDG.rhel7 pgdg94
postgresql95-server.x86_64               9.5.17-1PGDG.rhel7 pgdg95
postgresql96-server.x86_64               9.6.13-1PGDG.rhel7 pgdg96
[root@mydbinfo ~]#


So, we can see we have starting from 9.2,9.4,9.5,9.6,10.8 & 11.3 (latest) we have


Here we will install 9.6 and will upgrade it to 10.8.


To install a specific version run below command


sudo yum install postgresql96-server postgresql96-contrib


Initialize the Database

After  the installation is done, we need to  initialize the database using the below command:



/usr/pgsql-9.6/bin/postgresql96-setup initdb

Start the Database

After initializing the database, we can start the service using

systemctl start postgresql-9.6.service


In Linux by default, a user named postgres is created once PostgreSQL is installed.
You can change the user’s password if required
(sudo passwd postgres)


To switch to PostgreSQL prompt


su - postgres


To login to postgresql


psql


Some basic commands,


To list the database
postgres=# \l

To use a database
postgres=# \c dbname

To show tables from a database
dbanme=# \d

To view a table structure
dbanme=# \d tblname


Example:-


postgres=# create database test;
CREATE DATABASE
postgres=# \l
                                 List of databases
  Name    | Owner   | Encoding |   Collate | Ctype    | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          | |          | |      | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          | |          | |      | postgres=CTc/postgres
test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

postgres=#
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=#
test=# create table tbl (id integer,name varchar(10));
CREATE TABLE
test=#
test=# \d
       List of relations
Schema | Name | Type  | Owner
--------+------+-------+----------
public | tbl  | table | postgres
(1 row)

test=# \d tbl
            Table "public.tbl"
Column |         Type | Modifiers
--------+-----------------------+-----------
id     | integer               |
name   | character varying(10) |

test=#

No comments: