In this book, we will be working with PostGIS. PostGIS is one of the most popular and powerful geospatial databases and has the bonus of being open source and freely available. PostGIS itself is actually an extension to the PostgreSQL relational database system—to use PostGIS from your Python programs, you first have to install and set up PostgreSQL, then install the PostGIS extension, and then finally install the psycopg2 database adapter for Python. The following illustration shows how all these pieces fit together:

PostGIS allows you to store and query against various types of spatial data, including points, lines, polygons, and geometry collections. PostGIS provides two different types of spatial fields that can be used to store spatial data:
Because the mathematics involved is much more complicated, not all spatial functions are available for geography fields, and the operations often take a lot longer. However, geography fields are much easier to use if your spatial data uses an unprojected coordinate system such as WGS84.
Let's go ahead an install PostGIS onto your computer and then look at how we can use PostGIS to create and work with a spatial database using Python.
PostgreSQL is an extremely powerful open source relational database system. The main web site for Postgres can be found at http://postgresql.org. How you install the Postgres database will depend on which operating system your computer is running:
PostgreSQL.pkg package file to install Postgres into your computer.Once you have installed Postgres, you can check whether it is running by typing psql into a terminal or command-line window and pressing the Return key. All going well, you should see the Postgres command line:
psql (9.4.4) Type "help" for help. postgres=#
If the psql command complains about user authentication, you may need to identify the user account to use when connecting to Postgres, for example:
% psql -U postgres
Many Postgres installations have a postgres user, which you need to select with the -U command-line option when accessing the database. Alternatively, you may need to use sudo to run psql as root, or open a command prompt as an administrator if you are running Microsoft Windows.
To exit the Postgres command-line client, type \q and press Return.
Our next task is to install the PostGIS spatial extension for Postgres. The main web site for PostGIS can be found at http://postgis.net. Once again, how you install PostGIS depends on which operating system you are running:
Note that this PostGIS installer requires the GDAL Complete package, which you should have already installed while working through Chapter 2, GIS.
To check whether PostGIS has been successfully installed, try typing the following commands into your terminal window:
% createdb test_database % psql -d test_database -c "CREATE EXTENSION postgis;" % dropdb test_database
The first command creates a new database, the second one enables the PostGIS extension for that database, and the third command deletes the database again. If this sequence of commands runs without any errors, then your PostGIS installation (and Postgres itself) is set up and running correctly.
psycopg2 is the Python database adapter for Postgres. This is the Python library you use to access Postgres from within your Python programs. The main web site for psycopg2 can be found at http://initd.org/psycopg.
As usual, how you install psycopg2 will vary depending on which operating system you are using:
psycopg2 from source. For instructions on how to do this, refer to http://initd.org/psycopg/docs/install.html.psycopg2 from the command line:pip install psycopg2
Note that you will need to have the Xcode command-line tools installed so that psycopg2 can compile.
psycopg2 from http://www.stickpeople.com/projects/python/win-psycopg.To check whether your installation worked, start up your Python interpreter and type the following:
>>> import psycopg2 >>>
If psycopg2 was installed correctly, you should see the Python interpreter prompt reappear without any error messages, as shown in this example. If an error message does appear, you may need to follow the troubleshooting instructions on the psycopg2 web site.