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. If you have data in your previous version of postgres that you'd like to retain, then this is not recommended. Instead, you'll have to use pg_upgrade or pg_upgradecluster.

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 postgresql-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

Note that we create the database with the same name as our account. This isn't necessary, but is useful as by default psql will try to login to a database with the same name as your username.

Now we can exit the postgres user and query normally!

postgres@computer:~$ logout
@corporatepiyush

This comment has been minimized.

Copy link

@corporatepiyush corporatepiyush commented Dec 26, 2017

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

@sitthykun

This comment has been minimized.

Copy link

@sitthykun sitthykun 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

@eloff eloff 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

@ptsayli ptsayli 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

@lugabo lugabo commented Mar 14, 2018

sudo service postgresql restart

@gustav1105

This comment has been minimized.

Copy link

@gustav1105 gustav1105 commented Mar 19, 2018

Thank you.

@MalikRumi

This comment has been minimized.

Copy link

@MalikRumi MalikRumi 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

@romeech romeech 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

@kendalvictor kendalvictor commented May 9, 2018

sudo apt-get install postgresql-10 :V

@egalot

This comment has been minimized.

Copy link

@egalot egalot commented May 13, 2018

Unfortunately I get this Issue:

@karianpour

This comment has been minimized.

Copy link

@karianpour karianpour 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

@sam1197 sam1197 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).

@shahhs

This comment has been minimized.

Copy link

@shahhs shahhs commented Nov 3, 2018

Great Resource. Let me try.

@kaushikacharya

This comment has been minimized.

Copy link

@kaushikacharya kaushikacharya 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>;

@rifki

This comment has been minimized.

Copy link

@rifki rifki commented Dec 23, 2019

May some typo:
Please use sudo apt-get install postgresql-10 postgresql-contrib-10

@Abdelaziz18003

This comment has been minimized.

Copy link

@Abdelaziz18003 Abdelaziz18003 commented Feb 3, 2020

@sam1197 and for all others that are having this error (E: Unmet dependencies) while installing postgres on 19.04 or 19.10.
In the /etc/apt/sources.list.d/pgdg file, just change the word bionic to disco or eoan.

@patrickalima98

This comment has been minimized.

Copy link

@patrickalima98 patrickalima98 commented Feb 8, 2020

@sam1197 and for all others that are having this error (E: Unmet dependencies) while installing postgres on 19.04 or 19.10.
In the /etc/apt/sources.list.d/pgdg file, just change the word bionic to disco or eoan.

Very thanks my friend!

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.