0

How to execute for each table in the list one fixed query replacing just %table% in PostgreSQL (like variables and loops)

sql = 'select * from $table$ where...some_filter'

$table = {tb1, tb2, tb3...}

Somenone could clarify?

2
  • PostgreSQL doesn't support dynamic SQL in its dialect directly (though I remembered that I had read this sweet little hack a long time ago), you would need to use an EXECUTE wrapped in a function if you want to stick to the DB server to make this work.
    – geozelot
    Commented Oct 2, 2020 at 21:40
  • Also, do these tables have the same structure? If so, consider a merge into a single table as that would be the idiomatic, simpler and more performant way.
    – geozelot
    Commented Oct 3, 2020 at 5:47

1 Answer 1

1

Though not specifically GIS related, I'm working on something similar, so here's a quick solution.

Assuming you're using psycopg2 and already have a connection and a cursor set up to your database:

#array of table names
tables = ['schools_current', 'schools_projected']

for x in tables:
        sql_statement = 'select * from dpsdata.' + x + ';' 
        #hard-coded schema name above so you'll have to set your own
        print sql_statement
        curSource.execute(sql_statement)
        rows = curSource.fetchall()
        print rows
5
  • If using psycopg2, for extra credit, check out the psycopg2.sql module, which contains means to do this sort of thing without nasty surprises when table names have funny characters in them: psycopg.org/docs/sql.html . (To be honest, I have never used it myself...) Commented Oct 4, 2020 at 7:59
  • I don't know how psycopg2 work's but I'll try to learn more about. Commented Oct 5, 2020 at 11:21
  • @CaioMattos are you trying to actually interact with the database using python? Or something else...? Commented Oct 5, 2020 at 15:37
  • @DPSSpatial I'm looking for some solution with PostgreSQL, unfortunately Python is not my expertise. Commented Oct 5, 2020 at 16:45
  • @CaioMattos give me some more details on the solution you're after? Perhaps update your question a little bit? we do a lot with python and postgresql... very powerful. psycopg2 is fantastic. If you give me an example of your workflow I can update the code I provided. Commented Oct 5, 2020 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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