Installing and Configuring PostgreSQL Database on Ubuntu Server

1- Introduction

Document is based on:
  • Ubuntu Server 16.x

  • PostGres 9.6.1

The post can be applied to Unbuntu Server with the version of 12, 14 or 16.
You can find out other versions of PostGres here:

2- Install PostGres

Ubuntu 16.4 / 16.10

sudo add-apt-repository "deb https://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main"

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

sudo apt-get update

sudo apt-get install postgresql-9.6

Ubuntu 14.04:

sudo add-apt-repository "deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main"

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

sudo apt-get update
 
sudo apt-get install postgresql-9.6

Ubuntu 12.04

sudo add-apt-repository "deb https://apt.postgresql.org/pub/repos/apt/ precise-pgdg main"

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

sudo apt-get update

sudo apt-get install postgresql-9.6
If you receive an error when installing, please see the appendix at the end of this post:
E: The method driver /usr/lib/apt/methods/https could not be found.
N: Is the package apt-transport-https installed?
E: Failed to fetch https://apt.postgresql.org/pub/repos/apt/dists/xenial-pgdg/InRelease
E: Some index files failed to download. They have been ignored, or old ones used instead.
 
Installing Postgres on Ubuntu Server 16.4:
Start postgres:
sudo service postgresql start

3- Set up a password for the user postgres

Firstly, you need to log in PostGres (With root right of Ubuntu).
sudo -u postgres psql
You have logged in PostGres successully:
In PostGres, "postgres" is root user, you can set up new password for this user:

ALTER USER postgres PASSWORD 'newpassword';
After changing password successfully, you can exit PostGres with the command "\q".
After the password of the user "postgres" has been set up, you can log in to the Postgres through the command:

psql -U postgres -h localhost
Enter the password that you set up above

4- Using PostGreSQL

Make sure that you log in Postgres with  user "postgres".
Run 3 following commands to create user, database, and grant all the privileges of using the database to the use that is newly created.
CREATE USER myuser with PASSWORD '123';

CREATE DATABASE mydb;

GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
See in pgAdmin3:

Connect to "mydb" database with user "myuser":

Exit PostGres:
Logon "mydb" database with user "myuser":

psql -U myuser -h localhost  -d mydb
Now you have connected to database "mydb":

Create table:

-- Create table Account
Create table Account (User_Name varchar(30), Full_Name varchar(64) ) ;

-- Insert 2 row to Account.

Insert into Account(user_name, full_name) values ('gates', 'Bill Gate');

Insert into Account(user_name, full_name) values ('edison', 'Thomas Edison');

-- Query
Select * from Account;
See on pgAdmin3:

5- Appendix: Fix Error

If you receive an error message:
E: The method driver /usr/lib/apt/methods/https could not be found.
N: Is the package apt-transport-https installed?
E: Failed to fetch https://apt.postgresql.org/pub/repos/apt/dists/xenial-pgdg/InRelease
E: Some index files failed to download. They have been ignored, or old ones used instead.

 
Run command:

sudo apt-get install apt-transport-https

View more categories: