How To Install and Use PostgreSQL on Ubuntu 16.04

PostgreSQL is a powerful, open source object-relational database system.It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness. It is one of the leading relational database used on production servers.

Ubuntu’s default repositories contain Postgres packages, so we can install these easily using the apt packaging system.

Since this is our first time using apt in this session, we need to refresh our local package index.
install the Postgres package and a -contrib package that adds some additional utilities and functionality:

$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib

Access PostgreSQL using following command:

$ sudo -u postgres psql

create new user. It will ask for few details while creating new user.

$ sudo -u postgres createuser --interactive
$ Enter name of role to add: test
$ Shall the new role be a superuser? (y/n) y

Create new database

$ sudo -u postgres createdb sammy

check the connection info

postgres=# conninfo  

I got following error while working with PostgreSQL

conquistador@Inspiron-3542:~$ sudo su - postgres  
postgres@Inspiron-3542:~$ psql  
psql: could not connect to server: No such file or directory  
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Use following commands to address this error:

conquistador@Inspiron-3542:~$ sudo apt-get remove --purge postgresql-9.6  
conquistador@Inspiron-3542:~$ sudo apt-get install postgresql-9.6  

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.