Wednesday, May 23, 2012

How to Backup and Restore an entire postgreSQL database that contains one or more PostGIS databases in two easy steps

How to Backup and Restore an entire postgreSQL database that contains one or more PostGIS databases in two easy steps


Problem: We have a PostgreSQL server on Windows Server 2008 that has 5 databases. One of which is a PostGIS spatial database. If you just do a complete server backup and restore, the PostGIS database doesn't restore correctly.


We wanted to upgrade to 64 bits, the latest V9 version of Postgresql from V8, so needed to do a complete backup and restore to the new server.


After a couple of days mucking around in the documentation, I found that the instructions here did not work, at least not for me.

PostGIS Hard Upgrade for Windows

http://trac.osgeo.org/postgis/wiki/UsersWikiWinUpgrade


Instead, I found a simple work around, just make sure the PostGIS database is created before the restore.

First. Do a Server backup from the old server using pgAdmin III

On the old server, the easiest way is to use PGAdmin III, right click on the server and choose “Backup Server...”


Easy.

Next, 

Transfer this file to the new server.

We will restore this file on the new machine. It includes all roles and databases. The restore is pretty ugly, but works.

However, before doing this restore, you have to create any PostGIS databases, and turn them into PostGIS databases. Otherwise, the restore won't work correctly for these.

Create the PostGIS database:
Right click on “Databases” in PGAdmin and select ” New Database”.

Create a new PostGIS database on the new database server in PgAdmin.



When this is done, highlight the OGI database and open an SQL Query window from pgAdmin. (the magnifying glass in the toolbar)

Run these commands:
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;



Also, open and execute the “legacy.sql” script in the SQL window. This is located here:
PostgreSQL913\share\contrib\postgis-2.0

From the start menu, run the PostGreSQL 9.1 | SQL Shell as Administrator

Log into the server, with Database OGI. Run the following commands:
\cd f:/ogi/backups This makes your current directory where the backup SQL file is
\i OGI_5-23-2012.sql This loads the backup SQL file into the OGI database.




You will get many errors and warnings, you can ignore them. For sanity, it is best to just minimize the SQL Shell window and wait for the process to complete.

When this has completed, you will have all the roles, tables etc that existed in the old server. This process will work for upgrading from any version of PostgreSQL/PostGIS to this one.









Tuesday, November 15, 2011

Geoserver and ADF Files (ArcGrid Binary, AIG)

I just downloaded the entire US in 1\3 arc second resolution, in 1 degree tiles and wanted to work with them in Geoserver.

I first tried to use the latest GEO-EXT and GDAL from (1.1.X)

http://java.net/projects/imageio-ext/downloads/directory/Releases/ImageIO-Ext

and followed the instructions here:
http://docs.geoserver.org/stable/en/user/data/gdal.html

Didn´t work. Use the 1.08 version instead.

Finally, got this:


Nice. Now to get shaded relief and contours!

Apparently Shaded relief isn´t here yet, but contours are:

http://geo-solutions.blogspot.com/2011/01/developers-corner-have-your-sld.html

Using the latest nightly build, and the 1.10 version of the geo-ext binaries and jars, with this sld I get:



Most of the way there...

  <?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
  xmlns="http://www.opengis.net/sld" xmlns:gml="http://www.opengis.net/gml"
  xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/sld ./StyledLayerDescriptor.xsd">
  <NamedLayer>
    <Name>contour_lines</Name>
    <UserStyle>
      <FeatureTypeStyle>
        <Transformation>
          <ogc:Function name="gs:Contour">
            <ogc:Function name="parameter">
              <ogc:Literal>data</ogc:Literal>
            </ogc:Function>
            <ogc:Function name="parameter">
              <ogc:Literal>levels</ogc:Literal>
              <ogc:Literal>1850</ogc:Literal>
              <ogc:Literal>1860</ogc:Literal>
              <ogc:Literal>1870</ogc:Literal>
              <ogc:Literal>1880</ogc:Literal>
              <ogc:Literal>1890</ogc:Literal>
              <ogc:Literal>1900</ogc:Literal>
              <ogc:Literal>1910</ogc:Literal>
              <ogc:Literal>1920</ogc:Literal>
              <ogc:Literal>1930</ogc:Literal>
              <ogc:Literal>1940</ogc:Literal>
              <ogc:Literal>1950</ogc:Literal>
              <ogc:Literal>1960</ogc:Literal>
              <ogc:Literal>1970</ogc:Literal>
              <ogc:Literal>1980</ogc:Literal>
              <ogc:Literal>1990</ogc:Literal>
            </ogc:Function>
          </ogc:Function>
        </Transformation>
        <Rule>
          <linesymbolizer>
            <stroke></stroke>
          </linesymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

Now, how to get all the 1 degree files (about 900) in a single imagemosaic so we can view contours anywhere. Also, how to create a generic style that will work anywhere. Seems that the Level parameter doesn´t would like you would think in the gs:Contour.



For the first issue, this was encouraging " In addition, if imageio-ext GDAL extensions are properly installed we can also serve all the formats supported by it like MrSID, ECW, JPEG2000, etc... See GDAL Image Formats for more information on how to install them." from here: http://docs.geoserver.org/latest/en/user/tutorials/image_mosaic_plugin/imagemosaic.html

First I thought to see just how smart the ImageMosaic software is by giving it the path above all the ADF subdirectories...Just what it would do. It took a long time but it worked! Awesomely awesome!