Installing Postgis in Ubuntu 16.04 LTS

Gear up Guys. Here, I am going to discuss new problem faced few days back.

While selecting data from table in gis database I was getting an error.

could not access file "$libdir/postgis-2.1": No such file or directory

Then I tried to install postgis-2.1 but I got a dependency error.

$ sudo apt-get install postgresql-9.3-postgis-2.1

The following packages have unmet dependencies: postgresql-9.3-postgis-2.1 : Depends: libgdal1h (>= 1.9.0) but it is not installable Depends: libgeos-c1 (>= 3.4.2) but it is not going to be installed Depends: liblwgeom-2.1.7 (>= 2.1.6) but it is not going to be installed Depends: libproj0 (>= 4.8.0-1) but it is not installable E: Unable to correct problems, you have held broken packages.

But when I tried to install dependencies, I got an error.

libgeos-c1v5 : Breaks: libgeos-c1 (< 3.4.2-8~) but 3.4.2-4ubuntu1 is to
be installed.

After browsing a lot, I was not able to resolve the problem. So I decided to upgrade Ubuntu from 14.04 to 16.04.

The command to upgrade Ubuntu is

$sudo do-release-upgrade

Then again I tried to install postgis.

$sudo apt-get update

After upgrading I got a notice. I was not able to install any package through apt-get.

N: Ignoring file '50unattended-upgrades.ucf-dist' in directory '/etc/apt/apt.conf.d/' as it has an invalid filename extension

To resolve it first check the difference between old file and upgraded file. If no difference then remove old file from there.

diff /etc/apt/apt.conf.d/50unattended-upgrades.ucf-old /etc/apt/apt.conf.d/50unattended-upgrades
sudo rm /etc/apt/apt.conf.d//50unattended-upgrades.ucf-old

Then again resynchronize the package index file by running update command.

$sudo apt install postgresql postgresql-contrib postgis postgresql-9.5-postgis-2.2

It will output like this in between.

Ver Cluster Port Status Owner Data directory Log file
9.5 main 5434 down postgres /var/lib/postgresql/9.5/main /var/log/postgresql/postgresql-9.5-main.log
Setting up postgresql (9.5+173) …

Congratulations, You have installed it unless in between no problem arrived. Here I am going to make osm server in Ubuntu 16.04.

I am following this link. I will describe that steps where you might face an error. Rest all is same.

All steps are almost same as discussed in previous blogs.

While logging into the psql command line I was getting an error.

amisha@amisha$sudo -u postgres psql
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?

After searching I came to know that I have to restart the postgresql.

$sudo service postgresql restart

After that, I tried to create extension postgis which gave an error.

postgres=# create extension postgis;

ERROR: could not open extension control file “/usr/share/postgresql/9.6/extension/postgis.control”: No such file or directory

This error came because I have postgresql-9.6 as well as postgresql-9.5. By default data_directory was 9.6 and there was not postgis.control file in it. Moreover it was present in the directory 9.5. Then I decided to change the default data directory.

I used this link to change it. They have explained it very nicely.

I want to change the data directory from /var/lib/postgresql/9.6/main to /var/lib/postgresql/9.5/main

All the commands written below are explained it in the link.

sudo systemctl stop postgresql
sudo systemctl status postgresql
sudo mv /var/lib/postgresql/9.6/main /var/lib/postgresql/9.6/main.bak

Now open the postgresql configuration file in your favourite editor. Mine is vim.

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

Look at the value of data directory here. Change it new value where you want like I want in the directory 9.5.

postres=# SHOW data_directory;

. . .
data_directory = ‘/var/lib/postgresql/9.6/main
. . .

Now start the postgresql again. You may check the status.

$sudo systemctl start postgresql

$sudo systemctl status postgresql

Now try to run the server again. Here, I got an error. Notice carefully there is difference between the two errors on a same command. Earlier there was no file or directory but now permission is denied.

$sudo -u postgres psql
psql: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?

Then I saw the port number to which postgresql currently is listening through the command given below.

$ netstat -tulpn | grep postgres


(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0* LISTEN 14571/postgres

There are two ways t solve this problem.

  1. To use port number every time.
  2. Change your port number permanently.

Using the first approach I ran the same command with port number popped up in the command.

$ sudo -u postgres psql -p 5434

I am able to login. Now able to create postgis extension also.

postgres=# create extension postgis;

But this approach can bring problem again as we have done change on the terminal not permanently.

So, to permanently change port.

  1. Stop postgresql server.
  2. Make change in the configuration file like here. Change the port number for which the postgresql server searching for. Save it.

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

$ sudo systemctl enable postgresql

Then again start the postgresql server.

The furthur explanation I will explain in my next blog.

Thank You


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s