Skip to content

Instantly share code, notes, and snippets.

@alistairewj
Last active January 4, 2023 19:36
Show Gist options
  • Star 74 You must be signed in to star a gist
  • Fork 33 You must be signed in to fork a gist
  • Save alistairewj/8aaea0261fe4015333ddf8bed5fe91f8 to your computer and use it in GitHub Desktop.
Save alistairewj/8aaea0261fe4015333ddf8bed5fe91f8 to your computer and use it in GitHub Desktop.
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'
    • 14.04 is end of life, but the unupdated archive might work: sudo add-apt-repository 'deb http://apt-archive.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 postgresql-contrib-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
Copy link

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

@sitthykun
Copy link

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

@eloff
Copy link

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
Copy link

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
Copy link

lugabo commented Mar 14, 2018

sudo service postgresql restart

@gustav1105
Copy link

Thank you.

@MalikRumi
Copy link

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
Copy link

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
Copy link

sudo apt-get install postgresql-10 :V

@egalot
Copy link

egalot commented May 13, 2018

Unfortunately I get this Issue:

@karianpour
Copy link

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
Copy link

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
Copy link

shahhs commented Nov 3, 2018

Great Resource. Let me try.

@kaushikacharya
Copy link

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
Copy link

rifki commented Dec 23, 2019

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

@Abdelaziz18003
Copy link

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
Copy link

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

@fuggfuggfugg
Copy link

Trying to install pg-10 on Ubuntu 14. Trusty isn't supported anymore (https://apt.postgresql.org/pub/repos/apt/dists/). Returns a 404 Not Found.

Is there another archive link available or my only option is to Upgrade to 16 (xenial) ?

@alistairewj
Copy link
Author

Ubuntu 14.04 reached end of life in April 2019 - try https://apt-archive.postgresql.org/pub/repos/apt/dists/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment