Setting up a spatial database
When accessing a database using psycopg2
, we first have to specify which database we are going to use. This means that the database must exist before your Python code can use it. To set everything up, we'll use the Postgres command-line utilities. Type the following into your terminal or command-line window:
% createdb world_borders
Tip
Don't forget to include the -U postgres
option, or sudo
, if you need to access Postgres under a different user account.
This creates the database itself. We next want to enable the PostGIS spatial extension for our database. To do this, enter the following command:
% psql -d world_borders -c "CREATE EXTENSION postgis;"
Now that we've set up the database itself, let's create the table within the database which will hold our spatial data. To do this, we're going to create a Python program called create_table.py
. Go ahead and create this file within your world_borders
directory, and enter the following into the file:
import psycopg2
We...