Skip to main content

Postgresql Installation

check if yum repo contains desird packages
-------------------------------------------
yum list all | grep -i postgres

[root@simba ~]# yum list all | grep -i postgres
postgresql.x86_64                     8.4.13-1.el6_3              @anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4
postgresql-contrib.x86_64             8.4.13-1.el6_3              @localyumserver
postgresql-devel.x86_64               8.4.13-1.el6_3              @anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4
postgresql-docs.x86_64                8.4.13-1.el6_3              @anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4
postgresql-jdbc.noarch                8.4.701-8.el6               @localyumserver
postgresql-libs.x86_64                8.4.13-1.el6_3              @anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4
postgresql-odbc.x86_64                08.04.0200-1.el6            @anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4
postgresql-plperl.x86_64              8.4.13-1.el6_3              @localyumserver
postgresql-plpython.x86_64            8.4.13-1.el6_3              @localyumserver
postgresql-pltcl.x86_64               8.4.13-1.el6_3              @localyumserver
postgresql-server.x86_64              8.4.13-1.el6_3              @anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4
postgresql-test.x86_64                8.4.13-1.el6_3              @localyumserver
postgresql.i686                       8.4.13-1.el6_3              localyumserver
postgresql-devel.i686                 8.4.13-1.el6_3              localyumserver
postgresql-libs.i686                  8.4.13-1.el6_3              localyumserver
qt-postgresql.i686                    1:4.6.2-25.el6              localyumserver
qt-postgresql.x86_64                  1:4.6.2-25.el6              localyumserver
qt3-PostgreSQL.i686                   3.3.8b-30.el6               localyumserver
qt3-PostgreSQL.x86_64                 3.3.8b-30.el6               localyumserver

----------------------------------------------------------------------------------------------------------------------------

install postgres packages
--------------------------------
yum install postgresql*
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process

or

yum install postgresql-server.x86_64

Path where Postgres is installed
-----------------------------------
/var/lib/pgsql/data
[root@simba pgsql]# ls
backups  data
Confiuration path of postgresql
=================================
/usr/lib64/pgsql
/usr/share/pgsql/contrib

The data cluster is created by default in /var/lib/pgsql/9.3/data and the extensions folder is in /usr/pgsql-9.3/share/extension/ and the binaries are in /usr/pgsql-9.3/bin.
-----------------------------------
Initialize
-----------------------------------
The first command (only needed once) is to initialize the database in PGDATA.

service <name> initdb

E.g. for version 9.4:
============================
service postgresql-9.4 initdb

OR in our case
=============================
[root@simba usr]# service postgresql initdb
Initializing database:                                     [  OK  ]
You have new mail in /var/spool/mail/root
Starting service

If the previous command did not work, try directly calling the setup binary, located in a similar naming scheme:

/usr/pgsql-y.x/bin/postgresqlyx-setup initdb

E.g. for version 9.4:
===========================================
/usr/pgsql-9.4/bin/postgresql94-setup initdb


RHEL 7.1+ and CentOS 7.1+ are a bit different. Use:
===================================================
postgresql-setup initdb


Startup
-----------------------------------------------------------------
If you want PostgreSQL to start automatically when the OS starts:

chkconfig <name> on

[root@simba usr]#chkconfig postgresql on

E.g. for version 9.4:
============================
chkconfig postgresql-9.4 on

In RHEL 7+, try:

systemctl enable postgresql
===========================

Control service
-----------------------------------------------------------------
To control the database service, use:

service <name> <command>

where <command> can be:

    start : start the database
    stop : stop the database
    restart : stop/start the database; used to read changes to core configuration files
    reload : reload pg_hba.conf file while keeping database running


E.g. to start version 9.4:
===========================
service postgresql-9.4 start

OR in our case
============================
[root@simba usr]# service postgresql start
Starting postgresql service:                               [  OK  ]
[root@simba usr]#
or
[root@simba usr]# /etc/init.d/postgresql start

With RHEL 7.1+ and CentOS 7.1+, Systemd is introduced. Use this instead:
========================================================================
 systemctl enable postgresql
 systemctl start postgresql

Removing
------------------------------------------------------------------------
To remove everything:

yum erase postgresql94*

Or remove individual packages as desired.
-----------------------------------------

Verify PostgreSQL Installation
------------------------------------------------------------------------
After completing above steps, you have installed PostgreSQL 9.4 on your server, Let’s login to postfix to verify that installation completed successfully.

Log Into the Postgres Database
==============================
By default, postgres creates a user and a database called postgres.

The default security scheme assumes that the postgres user will only be accessible to those who need access. This can be controlled by setting up sudo rules.

We interact with the postgres database software through an interface called psql. It provides a prompt where we can manipulate and query for data.

log into the postgres user
Your prompt will change. You can now connect to the database that matches your username by typing:

-------------------------------------
[root@simba usr]# su - postgres
-bash-4.1$ psql
psql (8.4.13)
Type "help" for help.

You may create password for user postgres for security purpose.
================================================================
postgres=# \password postgres


List of roles
==============================================================================
postgres=# \du
           
Role name | Attributes  | Member of
-----------+-------------+-----------
 postgres  | Superuser   | {}
           : Create role  
           : Create DB    

List of databases
================================================================================
postgres=# \l

   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileg
es  
-----------+----------+----------+-------------+-------------+------------------
-----
 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/post
gres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/post
gres
(3 rows)

If you need to exit this interface, you can type this at any time:
postgres=# \q

Afterwards, to get back to the root user shell, you can type:
exit


Some Helpful psql Commands
=================================================================================
Here are a few commands that can help you get an idea of your current environment:

    \?: Get a full list of psql commands, including those not listed here.

    \h: Get help on SQL commands. You can follow this with a specific command to get help with the syntax.

    \q: Quit the psql program and exit to the Linux prompt.

    \d: List available tables, views, and sequences in current database.

    \du: List available roles

    \dp: List access privileges

    \dt: List tables

    \l: List databases

    \c: Connect to a different database. Follow this by the database name.

    \password: Change the password for the username that follows.

    \conninfo: Get information about the current database and connection.

Comments

Popular posts from this blog

20 perl programming tips

1. List all Installed Perl Modules from Unix Command Line Get a list of all installed perl modules as shown below. $ perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC' /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/Filter.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/LinkExtor.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/PullParser.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/Parser.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/TokeParser.pm ..... In the above example, File::Find  and  File::Spec::Functions  module are used to list all installed modules. -M option  loads the module. It executes  use module  before executing the script -T option  enables taint checking, which instructs perl to keep track of data from the user and avoid doing anything insecure w...

Perl and Excel: How to Read, Write, Parse Excel Files using Perl

If you want to manipulate excel files programmatically, you can use Perl Spreadsheet module, which provides an object interface that makes it easier to create and parse Excel files. Install Spreadsheet WriteExcel Module Method 1: Standard install using make Download the zipped tar file of  Spreadsheet-ParseExcel  and  Spreadsheet-WriteExcel  from cpan. Untar and unzip the module as follows: tar -zxvf Spreadsheet-WriteExcel.tar.gz cd to the directory the tar creates. Execute the steps below to to install the Spreadsheet-WriteExcel module. perl Makefile.PL make make test make install Use the above procedure to install Spreadsheet-ParseExcel also. Method 2: CPAN.pm install If you have CPAN.pm configured you can install the module as follows: perl -MCPAN -e 'install "Spreadsheet::WriteExcel"' perl -MCPAN -e 'install "Spreadsheet::ParseExcel"' For additional installation help refer to: How To Install Perl Modules Manually and Using ...

How to Connect to MySQL from Perl

How do I connect to a MySQL database from a perl program? use perl DBI module to connect to a MySQL database as explained below. If you don’t have perl DBI and DBD::mysql module installed, install perl module using cpan as we discussed earlier. # perl -MCPAN -e shell cpan> install DBI cpan> install DBD::mysql On a very high level, you’ll have to do the following three steps to connect to a MySQL database and get data. 1. Connect to the MySQL Database In the DBI module, you’ll use the connect function as shown below. $dbc = DBI->connect($source, $username, $password) DBI->connect function takes the following three arguments: $source – This is in the format of “DBI:mysql:[database]:[hostname]”. Replace the [database] and [hostname] with values from your system. In the example shown below, it is connecting to the database called “mycompany” that is running on the localhost. $username – The username that is used to connect to the MySQL database. $pass...