Use Postgres advanced types in Python
Postgres has a lot of useful builtin data types, but only some of them are mapped to Python types when accessing the DB using psycopg2.
Extending the support to other types is not straightforward, and involves the following steps:
- Create a Python class to store the data, e.g.
- Write a function to convert a
Pointto its SQL string representation, e.g.
- Write the inverse function to parse the SQL string representation of a
Pointand return and instance of a
- Finally bind all these functions and types, see
The complete code is as follows, also available as a gist: