Simulmedia / pyembedpg

A simple way to use Postgres in Python integration tests.
Apache License 2.0
34 stars 8 forks source link

Adding extensions? #5

Closed gamesbook closed 3 years ago

gamesbook commented 3 years ago

What approach is recommended / possible to add extensions that are needed for testing? For example, https://postgis.net/install/ is one we commonly use.

kjmph commented 3 years ago

For pyembedpg to work, it downloads PostgreSQL source code and builds it on the fly. It then proceeds to cache this build for subsequent use. If you would like to build an extension; a similar idempotent setup phase would need to execute after PyEmbedPG is instantiated; yet before it is started. Then, the normal CREATE EXTENSION calls would need to be made once the database is started.

As a rough example, lets say we want to install PostGIS. The version of PostgreSQL is installed by pyembedpg. Then, PostGIS needs to have the dependencies for that extension installed. Something like this needs to execute in the source directory for the extension (replace $PG_VERSION with the version string requested in pyembedpg:

$ PATH=$PATH:~/.pyembedpg/$PG_VERSION/bin ./configure
$ make
$ make install

Subsequent executions of pyembedpg using this cached directory will pick up PostGIS. However, the extension still needs to be created in the test suite's database. As root is the default PG Admin, the following would need to execute (modified to the test configuration):

        with psycopg2.connect(database='postgres', user='root', host='localhost', port=self.port) as conn:
            with conn.cursor() as cursor:
                cursor.execute('CREATE EXTENSION postgis')

As the dependency chains get quite in-depth with extensions; we leave including extensions as an exercise for consumers of this library.

Is this helpful?

kjmph commented 3 years ago

I'll go ahead and mark this closed.

gamesbook commented 3 years ago

Thanks; as often happens I got pulled off this project. Will get back to it at sometime & give feedback.

kjmph commented 3 years ago

If there are any clarifying answers we can give, please let us know.