Configure PostgreSQL in ubuntu and connect with DataGrip

PostgreSQL is the open source relational database system. PostgreSQL has some advanced features of database like 

  • User-defined types
  • Table inheritance
  • Sophisticated locking mechanism
  • Foreign key referential integrity
  • Views, rules, subquery
  • Nested transactions (savepoints)
  • Multi-version concurrency control (MVCC)
  • Asynchronous replication

Here I will explain how to configure PostgreSQL on Ubuntu Server and connect with your local machine. 

First of all, you need a ubuntu server and connect your server using SSH. When you are connected with SSH use the following commands to install PostgreSQL on Ubuntu.

Update apt-get and install PostgreSQL:
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

PostgreSQL is installed on your Ubuntu server. Currently, your database user is Postgres that has all database related privilege. Now you need to create a new user with a password so that you can connect the database using your app or any database connection tool like DataGrip.

To create a user in Postgres you first need to create a user on your server with the same name. So let's create a user in ubuntu server:

sudo adduser sheetal

 

I have created a user named sheetal. 

Now I will create a new user and database named sheetal. So first you need to switch your user to postgres.

sudo -i -u postgres

Now your current active user is postgres.

createuser sheetal -P

This will ask for a password to be set for the user. Please remember this password, this will require to connect your database.

Now create a database:

createdb sheetal

You have a user, password and a database. Now if you server port 5432 is now publicly open then open it your server firewall and security setting. So that you can connect the database publicly.

Exit from postgres user now:

exit

Now you need to update postgres configuration to allow users to connect the database from anywhere.

First update:

sudo vim /etc/postgresql/9.5/main/pg_hba.conf

Add the following:

host all all 0.0.0.0/0 md5

Second update:

sudo vim /etc/postgresql/9.5/main/postgresql.conf

Uncomment the listen_addresses setting and set *:

listen_addresses='*'

Now restart your Postgres service.

/etc/init.d/postgresql restart

 

All is done on the server side. Now open DataGrip on your local system and connect.

 

Now you are live with your database. Enjoy the successful connection.

Keywords: