Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add a POSTGIS extension in a test database ? #24

Open
AasheeshT opened this issue May 20, 2018 · 2 comments
Open

How to add a POSTGIS extension in a test database ? #24

AasheeshT opened this issue May 20, 2018 · 2 comments

Comments

@AasheeshT
Copy link

I am writing unit test for a function that makes a database connection and retrieves the SRID and Geometry type from a Postgres database ( having POSTGIS extension ) , How can i make an instance with a PostGIS extension?

@cleppanen
Copy link

cleppanen commented Jun 19, 2019

installing PostGIS can be done in code.
Here is a snippet from how we've achieved this. tmp_database is another fixture that does the initialization of the postgres database.

def tmp_postgis_db_(tmp_database):
    """
    This fixture initialize the postgis extension in the temporary database
    """

    # Creates a connection with autocommmit to save us from a few lines.
    conn = connect(url=tmp_database.url, autocommit=True)
    cur = conn.cursor()
    cur.execute("CREATE EXTENSION postgis")
    cur.close()
    conn.close()

    yield tmp_database

@robertlayton
Copy link

robertlayton commented Feb 15, 2023

Posting here for those coming from google. The code above works, but if you are using different database connection code, ensure you commit after the CREATE EXTENSION postgis line.

For example, using sqlalchemy:

        with db.engine.connect() as connection:  # or however you do it
            connection.execute(text("CREATE EXTENSION postgis;"))
            connection.commit()  # Very important!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants