Connecting to the database and creating a table

You should have created a database when you installed PostGIS. For the examples mentioned as follows, we will use this database.

If you did not create a database during the installation of PostGIS, you can do so using your terminal (command prompt in Windows) and the commands as follows:

createdb -U postgres pythonspatial psql -U postgres -d pythonspatial -c "CREATE EXTENSION postgis;" 

You may need to modify your path. On Windows, the command to do so is shown as follows:

set PATH=%PATH%;C:\Program Files\PostgreSQL\10\bin

To connect to your database, use the following code:

import psycopg2connection = psycopg2.connect(database="pythonspatial",user="postgres", password="postgres")cursor = connection.cursor() ...

Get Mastering Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.