I have two shapefiles - test7 and MD_Overlay
test7 has about 600,000 records with over one hundred attributes (about ~2GB) , so performing spatial joins in QGIS is very inefficient. I would like to find out the equivalent SQL command of QGIS Join attributes by location in PostGIS. My aim is to add the columns of MD_Overlay to test7 and to keep both matching and non-matching records.
This would be a polygons in polygons operation. (So a polygon in test7 would have to be fully within a polygon in MD_Overlay in order to be joined)
I have done much research but cannot find a solution. Any advice?

t.geom– Evan Carroll Nov 30 '16 at 08:59ERROR: GEOSContains: TopologyException: side location conflict at 2430764.1748952293 7027762.7618717942is there any way to ignore errors and continue? – iskandarblue Nov 30 '16 at 09:53WHERE ST_IsValid(t.geom) AND ST_IsValid(m.geom). Note that this will exclude records with invalid geometry from the result entirely. http://postgis.net/docs/ST_IsValid.html – alphabetasoup Nov 30 '16 at 10:13test7I'm just assuming the result isn't too important... otherwise it's important to invest a little bit of time to do things correctly. – alphabetasoup Nov 30 '16 at 10:31SELECT t.*, m.* FROM test7 AS t LEFT JOIN MD_Overlay AS m ON ST_Within(t.geom, m.geom) WHERE ST_IsValid(t.geom) AND ST_IsValid(m.geom)PGADMIN freezes and my computer overheats. However, the exact sameJoin attributes by locationis successful in QGIS. In QGIS, a new shapefile is created in the process. Perhaps I should be creating a new table in PostGIS? – iskandarblue Nov 30 '16 at 17:30LIMIT 100at the end of the query. – alphabetasoup Nov 30 '16 at 18:35SELECT t.*, m.* FROM test7 AS t LEFT JOIN MD_Overlay AS m ON ST_Within(t.geom, m.geom) WHERE ST_IsValid(t.geom) AND ST_IsValid(m.geom) LIMIT 100: Thank you. This operation worked successfully and 100 records were returned. However, I notice that there are now two geometry columns in the resulting table. How would one create a separate table with the results of all records and when I ready to export back out to shapefile, which geom should be used? – iskandarblue Nov 30 '16 at 21:04CREATE TABLE first_join AS SELECT t.*, m.* FROM test7 AS t LEFT JOIN MD_Overlay AS m ON ST_Within(t.geom, m.geom) WHERE ST_IsValid(t.geom) AND ST_IsValid(m.geom), I receive the following errorERROR: column "gid" specified more than once. In fact, 3 columns have the same name in both tables. Is there a good workaround? – iskandarblue Nov 30 '16 at 21:52SELECT x.col1, x.col2, y.col1etc. Using.*means every column. – alphabetasoup Nov 30 '16 at 21:59