【发布时间】:2016-12-26 00:03:09
【问题描述】:
我正在使用 shp2pgsql 将 shapefile 加载到 postGIS 数据库中,通过 psql 进行管道传输,包装在这样的 python 子进程中:
command = "shp2pgsql -s 4269 -a -D -W LATIN1 file.shp table | psql -h host -d db -U user"
p=subprocess.Popen(command, shell=True)
p.communicate()
这很完美,输出如下:
Loading objects...
Shapefile type: Polygon
Postgis type: MULTIPOLYGON[2]
SET
SET
BEGIN
COMMIT
没有END 声明,但据我所知END 和COMMIT 是等价的。
然后我想设置con.autocommit = True 用于与同一数据库的 psycopg2 连接。我收到以下错误:
psycopg2.ProgrammingError: autocommit cannot be used inside a transaction
为什么 psycopg2 报告事务仍在进行中?我应该以不同的方式关闭 psql 事务吗?
如果我不运行 shp2pgsql 子进程命令,con.autocommit 会成功执行。 shp2pgsql 默认情况下会在某处打开事务吗? (http://www.bostongis.com/pgsql2shp_shp2pgsql_quickguide.bqg 不建议这样做)
pg_locks 中不存在表明交易停滞/空闲的相关条目。我不在 shp2pgsql 函数中使用 psycopg2 连接对象。而且,如果我重新创建一个新的连接对象
con = psycopg2.connect(host=db_host, user=db_user, password=db_pass, database=db_name)
在 shp2pgsql 函数之后,con.autocommit=True 工作正常。
编辑:我当然可以在所有 shp2pgsql 导入完成后简单地创建 psycopg2 连接对象,但这在我的代码中并不理想,我宁愿了解发生了什么。
Edit2:在打开 psycopg2 连接后立即设置con.autocommit=True,而不是稍后设置,绕过此错误。
Edit3:添加 MWE
import psycopg2
import os
import subprocess
from glob import glob
def vacuum(con, table=""):
autocommit_orig = con.autocommit
con.autocommit = True
with con.cursor() as cur:
cur.execute("VACUUM ANALYZE {};".format(table))
con.autocommit = autocommit_orig
def read_shapefile(path, tablename, srid="4269"):
command = "shp2pgsql -s {} -a -D -W LATIN1 {} {} | psql -h {} -d {} -U {}".format(srid, path, tablename, host, dbname, user)
p=subprocess.Popen(command, shell=True)
p.communicate()
def load_data(con, datapath):
dir = os.path.join(datapath,dataname)
shapefiles = glob(os.path.join(dir,"*.shp"))
for shapefile in shapefiles:
read_shapefile(shapefile, tablename)
if __name__ == "__main__":
con = psycopg2.connect(host=db_host, user=db_user, password=db_pass, database=db_name)
load_data(con, datapath)
vacuum(con, tablename)
【问题讨论】:
-
你能为此发布一个 MWE 吗?我想确定我了解代码的布局方式。
-
我在原帖上面加了一个MWE。
标签: python postgresql psycopg2 psql