2

I'm creating the following table with a postGIS geometry column in python but get an error psycopg2.ProgrammingError: type "geometry" does not exist What should I use to be able to use postGIS in python?

import psycopg2

def main():
        conn_string = "host='localhost' dbname='postgres' user='postgres' password='postgres'"
        conn = psycopg2.connect(conn_string)
        conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
        cursor = conn.cursor()


        createTable = """  CREATE TABLE map
                        (
                            id INT PRIMARY KEY DEFAULT NULL,
                            geom GEOMETRY DEFAULT NULL,
                        );
                        """
        cursor.execute(createTable)
        conn.commit()
        cursor.close()
        conn.close()
if __name__ == "__main__":
    main()
  • 1
    Did you "Create Postgis Extension" ? CREATE EXTENSION postgis; – WKT Apr 6 '16 at 9:32
  • Are you working with postgres database as your conn_string shows? Try to create a database "mydb" and create the postgis extension. After that try to run your sql. If it works, go to python... – WKT Apr 6 '16 at 9:41
  • Great, indeed I didn't know how to extend in postGIS. Thanks i posted the right answer – Alexis K. Apr 6 '16 at 9:48
2

to do so you only need to add the postgis extension.

cursor.execute("CREATE EXTENSION postgis;")

and then create the table

createTable = """  CREATE TABLE map
                        (
                            id INT PRIMARY KEY DEFAULT NULL,
                            geom GEOMETRY DEFAULT NULL,
                        );
                        """
        cursor.execute(createTable)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.