Personal tools
You are here: Home North Carolina Coastal Ocean Observing System Members cbc How I Installed PostGIS
Document Actions

How I Installed PostGIS

A recipe for Fedora Core 4.

Table of Contents

OverviewGeosProj4PostGISphpPgAdminFire it up!Create a superuserConfigure PostGresCreate a spatially enabled databaseCreate a table with geometry


These are the steps I used on a custom "everything" install of Fedora Core 4. The everything install is used because so very many applications have compilation dependencies on PostGres if those applications are going to be used with PostGres, while those applications in turn also have complicated inter-dependencies with many other applications. Compiling PostGres separately outside the repository tree means compiling many other native Fedora applications outside the repository tree, creating a maintenance and dependency nightmare.

Do not attempt these procedures on an SELinux-enabled instance of Fedora unless you are intimately familiar with changing security contexts, defining security roles, compiling security policies, and loading security policies into the kernel. Otherwise, commands will appear to execute without error, but will not actually have had any effect. Simply disabling SELinux on a previously enabled instance of Fedora will also not work.

Until recently, there were no suitable RPMs for PostGIS, necessitating complicated and interdependent build processes outside the Fedora repositories. This is because PostGIS requires the PostGres source tree used to build PostGres in order to build PostGIS.

Recent work by Silke Reimer at through and the European Free Software Foundation. Silke has created Geos, Gdal, and PostGIS source RPMS for Fedora Core 4. Proj4 is available through the usual Fedora YUM repository and is prerequisite for PostGIS.

These SRPMS are in review phase at the Fedora Project and will be added to the Fedora "extras" YUM repository upon final approval. We are currently using Geos and PostGIS out of Fedora's Bugzilla until they are incorporated into the Fedora YUM repository proper. These SRPMS have only become available very recently, enable us to use PostGres in the Fedora YUM repository natively, and avoid many dependency and configuration pitfalls.


Geos provides PostGIS with spatial predicate functions and operators. Login as a user with su privileges and then:

cd /usr/src/redhat/SRPMS
rpmbuild --rebuild geos-2.1.4-1.src.rpm
cd ../RPMS/i386/
rpm -ivh geos-2.1.4-1.i386.rpm
rpm -ivh geos-devel-2.1.4-1.i386.rpm
rpm -ivh geos-doc-2.1.4-1.i386.rpm


Proj4 is a prerequite to the PostGIS SRPM. Proj4 provides ellipsoid transform functions to PostGIS. Continue from the same directory as root in the previous section:

yum install proj-devel


Continue from the same directory as root in the previous section:

cd ../../SRPMS/
rpmbuild --rebuild postgis-1.0.4-2.src.rpm
cd ../RPMS/i386/
rpm -ivh postgis-1.0.4-2.i386.rpm


We can take advantage of our native Apache and PHP enabled for PostGres in our Fedora installation by installing phpPgAdmin. phpPgAdmin gives us a highly secure (local connection) web based administration tool for our PostGres instance. Continue as root:

cd /var/www/html
mkdir -p unlikely/phpPgAdmin
tar -xzvf phpPgAdmin-4.0.1.tar.gz -C unlikely/phpPgAdmin
cd unlikely/phpPgAdmin/phpPgAdmin-4.0.1
mv * ..
cd ..
rmdir phpPgAdmin-4.0.1/

Note: phpPgAdmin will be accessed via the unlikely subdirectory in the URL of phpPgAdmin. The reason is that script kiddies routinely look for a phpPgAdmin subdirectory in the root HTML directory of your webserver. Your security and phpPgAdmin will keep them out. But there's no point in either making it easy on them or wasting your bandwidth letting them poke around. Make your unlikely subdirectory whatever you wish, but don't call it unlikely.

Note: you may wish to change conf/ in the phpPgAdmin directory later to suit changes in your PostGres configuration. Consult the phpPgAdmin documentation for details.

Note: the SourceForge mirror URL in the wget command above may possibly be unavailable. Check the phpPgAdmin website for download locations.

Fire it up!

Now it is a good idea to make the native PostGres and Apache available for configuration and testing. Make them restart on each reboot also with chkconfig. Continue as root:

/sbin/service httpd start
/sbin/service postgresql start
/sbin/chkconfig httpd on
/sbin/chkconfig postgresql on

Note: if you are starting the postgresql service for the first time on your PostGres instance, the postgresql service will initialize the database cluster with initdb automatically into /var/lib/pgsql.

Note: because you have su as root instead of logged in as root, you do not have /sbin in your $PATH and need to specify it explicitly for system commands.

Note: port 80 needs to be open for external access to Apache. Refer to iptables documentation if you need help. Generally, add the following line before the last line in /etc/sysconfig/iptables:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

save, and then restrart iptables:

/sbin/service iptables restart

Create a superuser

While PostGres is still configured in the default mode which uses the system's IDENT server for authentication, we can take advantage of our su privileges to masquerade as the built in postgres system user provided by Fedora and create a PostGres superuser using PostGres command line utilities.

PostGres has its own user space. That is, PostGres does not use the users managed by Linux. However, it is generally a good idea is you can keep PostGres and Linux users and passwords in sync. So we will create a Linux user (called superman for purposes of illustration) with enough superpowers to manage PostGres in a pinch, as well as create a PostGres user to match. Call your PostGres (and Linux) superuser something besides superman. You should only use this superuser for maintenance, and not for application connections.

Continue as root:

/usr/sbin/useradd superman -m -G users,wheel,postgres -s /bin/bash
passwd superman
su postgres
createuser -a -d -E -P -e superman

Note: the -e option, which echoes the commands createuser sends to the database server, will cause the password to the new user to be echoed to stdout. Omit the -e option if you do not have a secure stdout environment.

Note: the -a option, which enables PostGres superuser authority to the new user, is the same as the -s option in other versions of PostGres.

Configure PostGres

Now that we have a superuser, notify PostGres we wish to use MD5 stored passwords in the future. Continue as root:

nano -w /var/lib/pgsql/data/pg_hba.conf

Change the following lines:

local all all              ident sameuser
host  all all ident sameuser
host  all all ::1/128      ident sameuser

To these:

local all all              md5
host  all all md5
host  all all ::1/128      md5

and save the file (with cntl-o then cntl-x). Finally:

/sbin/service postgresql restart

Create a spatially enabled database

The folowing commands will create a test database, enable the procedural language for that database, load the PostGIS functions into the database, and create an EPSG spatial reference table in the database. Continue as root:

createdb -U superman -W testdb
createlang -U superman -W plpgsql testdb
psql -U superman -W -d testdb -f /usr/share/pgsql/contrib/lwpostgis.sql
psql -U superman -W -d testdb -f /usr/share/pgsql/contrib/spatial_ref_sys.sql

Create a table with geometry

We can now use phpPgAdmin to interactively execute some SQL commands and view the results.

  • From a web browser visit, http://yourserver.yourdomain/unlikely/phpPgAdmin (substituting yourserver.yourdomain and unlikely).
  • Click on the blocked out PostGreSQL icon in the left hand navigation.
  • Enter the PostGres userid and password you created and click on Login.
  • Click on the blocked out PostGreSQL icon in the left hand navigation.
  • Click on the testdb link for the test database you created.
  • Click on the SQL tab.
  • Enter the following SQL and click Run:
CREATE TABLE geotable ( ID int4, NAME varchar(20) );
SELECT AddGeometryColumn('', 'geotable','geom',-1,'LINESTRING',2);
   VALUES (1,
           'First Geometry',
            GeomFromText('LINESTRING(2 3,4 5,6 5,7 8)', -1));
SELECT id, name, AsText(geom) AS geom FROM geotable;

If you see something which looks like the following, congratulations, you have successfully installed PostGIS:

Query Results
id  name        geom
1   First Geometry  LINESTRING(2 3,4 5,6 5,7 8)

1 row(s)

Total runtime: 97.289 ms

SQL executed.


Posted by topping at 2006-05-26 19:35

These instructions worked almost flawlessly! I can't imagine how unappealing this install would have been without them. I certainly owe the author a fizzy beverage at some point.

The only major change I needed to make was adding yum install postgresql-devel before rpmbuild --rebuild postgis-1.0.4-2.src.rpm. It was relatively obvious that this needed to be done, but might not be clear to a DBA that doesn't have basic Fedora admin skills.

Thanks again!

Powered by Plone, the Open Source Content Management System