Creating the database

Generating the database table is made possible by the Chapter12_0.py script, which creates a PostgreSQL database called chapter12 and adds spatial functionality to the new database. Adjust the credentials, host, and port (as needed) in the connection configuration below.

Connect to the database server using psycopg2 and its connect function, which creates a connection class. The class has a cursor function that creates a cursor object, which is able to execute SQL statements. This section creates the database for the chapter:

import psycopg2
connection = psycopg2.connect(host='localhost', user='{user}',password='{password}', port="5432")
connection.autocommit = True
cursor = connection.cursor()
cursor.execute('CREATE DATABASE chapter12')

To make the database geospatial, ensure that the PostGIS spatial add-on has been installed. Connect to the new database and pass the following SQL statement, which adds the spatial functionality tables to the database:

import psycopg2
connection = psycopg2.connect(dbname='chapter12', host='localhost', user='{user}', password='{password}', port="5432")
cursor = connection.cursor()
connection.autocommit = True
cursor.execute('CREATE EXTENSION postgis')
connection.close()

The PostGIS database for this chapter is now created and spatially enabled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset