Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Normal Postgres Database to PostGis Database

I have a normal postgres database with lots of geo coded data. This is just present two columns as latitude and longitude.

I want to convert this database to PostGIs database. Can anyone suggest me a way to convert the database i have? I don't want to create a new postgis tempalte based database and then move the whole data one by one.

like image 709
Ibtesam Hussain Sharif Avatar asked Feb 24 '13 06:02

Ibtesam Hussain Sharif


2 Answers

First, make sure PostGIS is installed on the system, then create a PostGIS extension for the database using:

CREATE EXTENSION postgis;

Next, spatially enable each table with a geometry column and populate the column with the long/lat data points:

ALTER TABLE mytable ADD COLUMN geom geometry(Point,4326);
UPDATE mytable SET geom = ST_SetSRID(ST_MakePoint(long, lat), 4326);

Also consider using the geography type:

ALTER TABLE mytable ADD COLUMN geog geography(Point,4326);
UPDATE mytable SET geog = ST_MakePoint(long, lat)::geography;
like image 179
Mike T Avatar answered Oct 14 '22 01:10

Mike T


I know this is older... but I'd add something to address a previous issue:

@Cerin, make sure you apt-get install postgresql-x.x-postgis-2.1, not just apt-get install postgis. I had that issue before as well

like image 20
jpmaniac87 Avatar answered Oct 14 '22 01:10

jpmaniac87