With a raster loaded into PostgreSQL, you can query it using Python. The Python library for working with PostgreSQL is psycopg2. The following code will connect to the database where you loaded the TIF:
import psycopg2
connection = psycopg2.connect(database="pythonspatial",user="postgres", password="postgres")
cursor = connection.cursor()
The previous code imports psycopg2. It then makes a connection passing the database name, username, and password. Lastly, it gets a cursor object so that you can execute queries.
To see the raster in PostgreSQL, you can execute a select all, as shown in the following code:
cursor.execute("SELECT * from bigi")
#Big I is the name of the intersection where I-25 and I-40 meet and split Albuquerque in quadrants.
cursor.fetchall()
The previous code executes a select all statement and prints all the results. There are two columns in the table—rid and rast. Rid is the unique ID field for the raster. If you had tiled it when running raster2pgsql, there would be more rows. The rast column holds the raster data:
[(1,
'010000010000000000000024400000000000002440D8B969334EA85AC0D82D02637D8D414000000000000000000000000000000000E61000000700040004000A0A010A0A0A0A010101320A0A320A0101330A0A3201010101320A32')]