Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install PostgreSQL 10 on Ubuntu

Install PostgreSQL 10 on Ubuntu

This is a quick guide to install PostgreSQL 10 - tested on Ubuntu 16.04 but likely can be used for Ubuntu 14.04 and 17.04 as well, with one minor modification detailed below.

(Optional) Uninstall other versions of postgres

To make life simple, remove all other versions of Postgres. Obviously not required, but again, makes life simple.

dpkg -l | grep postgres

returned for me:

ii  postgresql                                  9.5+173                                                     all          object-relational SQL database (supported version)
ii  postgresql-9.5                              9.5.8-0ubuntu0.16.04.1                                      amd64        object-relational SQL database, version 9.5 server
ii  postgresql-client-9.5                       9.5.8-0ubuntu0.16.04.1                                      amd64        front-end programs for PostgreSQL 9.5
ii  postgresql-client-common                    173                                                         all          manager for multiple PostgreSQL client versions
ii  postgresql-common                           173                                                         all          PostgreSQL database-cluster manager
ii  postgresql-contrib-9.5                      9.5.8-0ubuntu0.16.04.1                                      amd64        additional facilities for PostgreSQL

... therefore I ran:

sudo apt-get --purge remove postgresql postgresql-9.5 postgresql-client-9.5 postgresql-client-common  postgresql-common postgresql-contrib-9.5

Add PostgreSQL apt repository

The current default Ubuntu apt repositories only have up to postgresql-9.6. To get 10, we'll add the official postgres apt repository.

  • Ubuntu 14.04: sudo add-apt-repository 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main'
  • Ubuntu 16.04: sudo add-apt-repository 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main'
  • Ubuntu 17.04: sudo add-apt-repository 'deb http://apt.postgresql.org/pub/repos/apt/ zesty-pgdg main'

Now import the repository signing key, followed by an update to the package lists:

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

Install PostgreSQL

sudo apt-get install postgres-10

Ensure that the server is started by switching to the postgres user.

sudo su - postgres
/usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -l logfile start

If that fails, the server might be running, so restart it to be safe: /usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -l logfile restart

That should return something like:

postgres@computer-name:~$ /usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -l logfile restart
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started

Which means your PostgreSQL 10 is up and running!

(Optional) Create a user for yourself

Since we're still logged in as the postgres user, now is a good time to create your own user account. This allows you to use operating system level authentication locally, greatly simplifying access to the database. I also like to create a database with my username, as this is the default database that psql will connect to, and if the database doesn't exist psql throws a pesky error (which gets me every time).

psql
CREATE ROLE <username> SUPERUSER LOGIN REPLICATION CREATEDB CREATEROLE;
CREATE DATABASE <username> OWNER <username>;
\q

Now we can exit the postgres user and query normally!

postgres@computer:~$ logout
@corporatepiyush

This comment has been minimized.

Copy link

commented Dec 26, 2017

install command should be
sudo apt-get install postgresql-10

@sitthykun

This comment has been minimized.

Copy link

commented Jan 26, 2018

Yes, It should be
sudo apt-get install postgresql-10
or
sudo apt install postgresql-10

@eloff

This comment has been minimized.

Copy link

commented Feb 11, 2018

Thanks for the guide, it was very helpful. But please update the install command as it caused me quite a bit of confusion (and I didn't see these comments here.)

@ptsayli

This comment has been minimized.

Copy link

commented Feb 13, 2018

I am getting following error while starting server
pg_ctl: could not read file "/var/lib/postgresql/10/main/postmaster.opts"

@lugabo

This comment has been minimized.

Copy link

commented Mar 14, 2018

sudo service postgresql restart

@gustav1105

This comment has been minimized.

Copy link

commented Mar 19, 2018

Thank you.

@MalikRumi

This comment has been minimized.

Copy link

commented Mar 20, 2018

These instructions say nothing about existing data - does it need to be set aside, it is not effected by these procedures, etc. I think it would be helpful to make that clear.

@romeech

This comment has been minimized.

Copy link

commented Apr 4, 2018

Thanks for the instruction. In the whole it helped me to deploy postgresql 10 in a fast manner. Nevertheless, I'd like to report a problem:

Command /usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -l logfile restart failed on my Ubuntu 16.04.4 LTS.

However, the alternative suggested by @lugabo
sudo service postgresql restart
worked perfectly. May be it deserves to be mentioned in the instruction?

(I also stumbled on apt-get install postgres-10 as the previous commenters but found the right name of the package by search. So it would be better to fix it too)

@kendalvictor

This comment has been minimized.

Copy link

commented May 9, 2018

sudo apt-get install postgresql-10 :V

@egalot

This comment has been minimized.

Copy link

commented May 13, 2018

Unfortunately I get this Issue:

@karianpour

This comment has been minimized.

Copy link

commented May 26, 2018

I needed to run the following command to be able to install postgres 10 on ubuntu 16.04 👍

echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' >> /etc/apt/sources.list.d/pgdg.list
However, the above command is very similar to the first command which is :
sudo add-apt-repository 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main'

@sam1197

This comment has been minimized.

Copy link

commented Sep 16, 2018

sudo apt-get install postgresql-10
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
libgssapi-krb5-2 : Depends: libkrb5-3 (= 1.15.1-2) but 1.13.2+dfsg-5ubuntu2 is to be installed
Depends: libkrb5support0 (>= 1.15beta1) but 1.13.2+dfsg-5ubuntu2 is to be installed
postgresql-10 : Depends: postgresql-client-10
Depends: postgresql-common (>= 182
) but it is not going to be installed
Depends: libicu60 (>= 60.1-1~) but it is not installable
Depends: libpq5 (>= 9.3~) but it is not going to be installed
Depends: libssl1.1 (>= 1.1.0) but it is not installable
Recommends: sysstat but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

@shah-hs

This comment has been minimized.

Copy link

commented Nov 3, 2018

Great Resource. Let me try.

@kaushikacharya

This comment has been minimized.

Copy link

commented Dec 14, 2018

@alistairewj
A minor correction:

In the last section, you probably wanted to write

CREATE DATABASE <dbname> OWNER <username>;

instead of

CREATE DATABASE <username> OWNER <username>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.