Python – How to Solve GeoPandas GeoDataFrame.to_postgis AttributeError

geodataframegeopandaspostgispostgresqlpython

I have this geopandas geodataframe which I want to write in the postgis/postgreSQL database.

I have followed the steps as shown here by the geopandas docs
https://geopandas.readthedocs.io/en/latest/docs/reference/api/geopandas.GeoDataFrame.to_postgis.html

I have the required library installed viz: SQLAlchemy, GeoAlchemy2, and psycopg2 (PostgreSQL Python driver)

But I am getting this error:

AttributeError: 'GeoDataFrame' object has no attribute 'to_postgis'

If somebody knows then, Please tell, how to rectify this.

Or what are the other methods to write/export the spatial data/geopandas data/ ESRI shapefile data to postgreSQL database?

The geodataframe was already written above this codeblock which I want to write in postgreSQL.

The code is as follows:

from sqlalchemy import create_engine

from geoalchemy2 import Geometry, WKTElement

import pandas as pd

import psycopg2

import geopandas as gpd


engine = create_engine('postgresql://postgres:imd123#@localhost:5432/abhilash')

shapefile_gdf_final.to_postgis(con=engine, name="table_name")

the code picture 1
the code picture 2

Best Answer

You are using old version of geopandas. Update to the latest 0.8.1, to_postgis is a new function which is not present in older versions.

conda install -c conda-forge geopandas==0.8.1