PostgreSQL upgrade on Amazon RDS blocked by PostGIS version - postgresql

I'm trying to upgrade my RDS instance from 9.5.4 to 9.6.1, and I'm getting blocked by an error about the PostGIS version needing to be upgraded first.
Database instance is in a state that cannot be upgraded: PreUpgrade checks failed: The instance could not be upgraded because there are one or more databases with an older version of PostGIS installed. Please upgrade all installations of PostGIS and try again.
The highest version of PostGIS supported by RDS 9.5.4 is 2.2.2
alter extension postgis update
yields
NOTICE: version "2.2.2" of extension "postgis" is already installed
I tried upgrading explicitly to the version in 9.6.1
alter extension postgis update to '2.3.0'
Getting
[Err] ERROR: extension "postgis" has no update path from version "2.2.2" to version "2.3.0"
So my question is: how do I upgrade? I suspect I can delete the extension prior to the upgrade and install the new version after, but I don't know what that will do to my data, or if that will properly upgrade my spatial structures or functions.
I have already checked the documentation for any mention of this scenario

Make sure your instance is upgraded to 9.5.4 before upgrading to 9.6.1.
I had this same error on one of my DB instances, but it was on 9.5.2. First I upgraded it to 9.5.4 and then to 9.6.1 and everything worked as expected.
The next instance I tried it on was already on 9.5.4 and got the same message.
So first I did an:
ALTER EXTENSION postgis UPDATE;
Then upgraded the server
With new versions of PostGIS from 2.x to 3.x try running this instead. (You have tor un it twice)
SELECT postgis_extensions_upgrade();
SELECT postgis_extensions_upgrade();

I was running into this exact issue trying to get a server upgraded from 9.3 to 9.6. Through a bit of trial and error I found a successful path. Just a detail about the upgrade process: after each upgrade, the RDS requires a manual reboot. I would think the upgrade would automatically handle that, but it does not.
Starting point:
SELECT version(); -> PostgreSQL 9.3.20 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
SELECT postgis_full_version(); -> POSTGIS="2.1.8 r13780" GEOS="3.5.0-CAPI-1.9.0 r4084" PROJ="Rel. 4.9.2, 08 September 2015" GDAL="GDAL 1.11.5, released 2016/07/01" LIBXML="2.9.1" LIBJSON="UNKNOWN" RASTER
ALTER EXTENSION postgis UPDATE; -> Returned success, but no change to PostGIS version
After upgrading to 9.4:
SELECT version(); -> PostgreSQL 9.4.18 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
SELECT postgis_full_version(); -> POSTGIS="2.1.8 r13780" GEOS="3.5.0-CAPI-1.9.0 r4084" PROJ="Rel. 4.9.2, 08 September 2015" GDAL="GDAL 1.11.5, released 2016/07/01" LIBXML="2.9.1" LIBJSON="UNKNOWN" RASTER
ALTER EXTENSION postgis UPDATE; -> Returned success, but no change to PostGIS version
After upgrading to 9.5:
SELECT version(); -> PostgreSQL 9.5.13 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
SELECT postgis_full_version(); -> POSTGIS="2.1.8 r13780" GEOS="3.5.1-CAPI-1.9.1 r4246" PROJ="Rel. 4.9.2, 08 September 2015" GDAL="GDAL 1.11.5, released 2016/07/01" LIBXML="2.9.1" LIBJSON="UNKNOWN" RASTER
ALTER EXTENSION postgis UPDATE; -> SQL Error [XX000]: ERROR: attempt to redefine parameter "postgis.backend"
So I was stuck here for a bit, couldn't seem to get PostGIS upgraded successfully. Then I saw this post describing that same error and decided to just wait for 24 hours to see if the RDS instance received a patch. It seems that it did, though I didn't notice anything in the logs. In any event, without any changes from myself, the next day I was able to successfully upgrade the PostGIS extension
ALTER EXTENSION postgis UPDATE; -> Returned success
SELECT postgis_full_version(); -> POSTGIS="2.2.5 r15298" GEOS="3.5.1-CAPI-1.9.1 r4246" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.0.3, released 2016/07/01" LIBXML="2.9.1" LIBJSON="0.12" RASTER
After that, I was able to upgrade the RDS instance to 9.6
SELECT version(); -> PostgreSQL 9.6.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
SELECT postgis_full_version(); -> POSTGIS="2.2.5 r15298" GEOS="3.5.1-CAPI-1.9.1 r4246" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.0.3, released 2016/07/01" LIBXML="2.9.1" LIBJSON="0.12" RASTER
ALTER EXTENSION postgis UPDATE; -> Returned success
SELECT postgis_full_version(); -> POSTGIS="2.3.7 r16523" PGSQL="96" GEOS="3.6.2-CAPI-1.10.2 4d2925d6" PROJ="Rel. 4.9.2, 08 September 2015" GDAL="GDAL 2.1.4, released 2017/06/23" LIBXML="2.9.1" LIBJSON="0.12" RASTER
I'd like to figure out how to trigger that apparent patch to the extensions immediately rather than just waiting for it, but I haven't been able to figure that out yet.

I had same issue. the problem was, I had more than 1 database. I upgraded main database but forgot about others. You need to check all your databases in same server and upgrade each of them with following command.
ALTER EXTENSION postgis UPDATE;

The real reason for the ERROR: attempt to redefine parameter "postgis.backend" is because you have referenced the old postGIS libraries by invoking the PostGIS_full_version() function, as a result the library configuration cannot be updated to the new version since the session still has the old functions loaded.
You need to disconnect from the database and connect a new session then immediately invoke the ALTER EXTENSION postgis UPDATE; command before trying to use any gis functions.
Any existing connections will reference the old functions until they disconnect & reconnect at which point the new ones will be loaded.
Once the AWS RDS PostgreSQL upgrade step is completed you should perform a further REBOOT of the instance in order to complete the synchronisation of the parameter group. I would recommend this be done before you do the postGIS extension updates.
You can check what version of the libraries are present by using:
select distinct probin from pg_proc where probin IS NOT null order by probin and probin like '%postgis%';

Related

postgres upgrade from 10.9 to 11.4 failure (aws rds)

postgres upgrade from 10.9 to 11.4 on aws rds failure with below reason
Database instance is in a state that cannot be upgraded: PreUpgrade
checks failed: The instance could not be upgraded because there are
one or more databases with an older version of PostGIS installed.
Please upgrade all installations of PostGIS and try again.
i have postgis 2.4.4 installed and command
ALTER EXTENSION postgis UPDATE;
fails with message
NOTICE: version "2.4.4" of extension "postgis" is already installed
and command
ALTER EXTENSION postgis UPDATE TO "2.5.1"
fails with this message
extension "postgis" has no update path from version "2.4.4" to version
"2.5.1"
In order to upgrade to version 2.5.* you need to run the following command
ALTER EXTENSION postgis UPDATE TO "2.5.2";
It gives you that error because there is no upgrade from 2.4.4 to 2.5.1. You can only move to 2.5.2 as next step.
Refer to this page under section "PostgreSQL version 10.x extensions supported on Amazon RDS".
And similarly if you wanted to go to 3.1.4 from 2.4.4, you must first go to 2.5.2 and then to 3.1.4.

Why does `pg_config --version` give a different version than `select version();` does?

I am attempting to identify what version of postgres server I have installed, and noticed this question on that.
The first answer there says to run SELECT version(); (in postgres), the second answer says to run pg_config --version (in the terminal). Somehow I get different versions when running each of these commands.
In Postgres:
=> select version();
version
------------------------------------------------------------------------------------
PostgreSQL 9.4.12 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 5.4.0, 64-bit
(1 row)
In the terminal:
$ pg_config --version
PostgreSQL 9.6.2
Why are different version numbers reported when running these two commands?
My machine is running NixOS.
As Abdel P. suggested, it turned out that I had both a global and a user-level postgres installed.
I uninstalled the user-level version and now both show version 9.4.12.

Which is the Postgres version

I have a doubt about my version of Postgres installed in my environment.
This is my os: CentOS Linux release 7.3.1611 (Core)
In /usr/ I can see: pgsql-9.4/ folder.
But when I do: select version(); I get:
PostgreSQL 9.2.18 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.8.5
20150623 (Red Hat 4.8.5-11), 64-bit
So, what version do I have?
To determine the version of the database server, use select version() (from a connection to the database):
postgres=# select version();
version
----------------------------------------------------------------------------------------------------------------
PostgreSQL 9.2.14 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16), 64-bit
To determine the version of the database client, use psql --version (from your shell command line):
$ psql --version
psql (PostgreSQL) 9.6.2
The server is where data is stored. The client is the software you use to connect to the server. They can be different versions.
Simply check the version by this command: psql --version
You may of course have more than one instance of Postgresql. Try this sql to show your current instance location in case there is more than one
SHOW data_directory;

Incompatible postgis 2.1 for postgresql 9.3.3 homebrew os x 10.10

I have upgraded from OS X 10.9 to 10.10 Yosemite. I was running postgresql 9.3.3 with postgis 2.1. I did not do a pgdump before upgrading to OS X 10.10 (I know, I know, kill me...) but I have the original data directory. I did a brew install postgresql and got 9.4.4. So I installed Postgresql 9.3.9 (the only 9.3 version I could find) with Homebrew. Also brew install postgis. I then got a version 2.1 Postgis for Postgresql 9.4.4. I tried to install postgis20 but that does not work with my database.
When I start Postgresql 9.3.3 I can see all my databases but when I browse I get the error:
ERROR: could not access file "$libdir/postgis-2.1": No such file or directory
I tried to link to posts-2.1 in the $libdir directory but then I get this message:
ERROR: incompatible library "/usr/local/Cellar/postgresql93/9.3.9/lib/postgis-2.1.so": version mismatch
DETAIL: Server is version 9.3, library is version 9.4.
So obviously I need a postgis-2.1 for Postgresql 9.3 but I don't know how to fix that.
If I try to run a pg_dumpall I get this error (obvious I guess):
➜ ./pg_dumpall >> old_backup.sql
pg_dump: Dumping the contents of table "darwin_test" failed: PQgetResult() failed.
pg_dump: Error message from server: ERROR: could not load library "/usr/local/Cellar/postgresql/9.3.3/lib/postgis-2.1.so": dlopen(/usr/local/Cellar/postgresql/9.3.3/lib/postgis-2.1.so, 10): Symbol not found: _json_tokener_errors
Referenced from: /usr/local/Cellar/postgresql/9.3.3/lib/postgis-2.1.so
Expected in: /usr/local/lib/libjson-c.2.dylib
in /usr/local/Cellar/postgresql/9.3.3/lib/postgis-2.1.so
So how do I get postgis-2.1 to work again with postgresql 9.3.3?
In homebrew I only find a postgis-2.1 for postgresql94.
Use brew edit postgis to change the postgresql references to postgresql93 in the install script and then run brew install postgis. You might also have to link/unlink postgresql using brew link postgresql93 or brew unlink postgresql.

\d command doesnt work anymore with psql-client 8.3 (upgrade to v9.0)

Since we upgrade our postgresql 8.3 to postgresql 9, \d command doesnt works anymore with psql-client 8.3 :
ERROR: column "reltriggers" does not exist
LINE 1: SELECT relhasindex, relkind, relchecks, reltriggers, relhasr"
In postgres trees psql-client must download as one package with postgresql9. Where i can download only psql-client binary package a.k.a psql for Postgresql 9.0 ?
Update:
This case occur when older psql-client trying to access a newer version of postgres (psql-client: 8.3 trying to access postgres-server: 9.0).
It appears that Ubuntu does not ship PostgreSQL 9.0 yet. So this could be a bit tricky. Either you build PostgreSQL 9.0 from source and only install the client parts that you want, or you download the Debian source package for postgresql-9.0 and build that on Ubuntu and then only install the postgresql-client-9.0 package.
Or you install the postgresql-client-8.4 package. This isn't quite the solution, but it's closer to it, and I verified that it will get you past the error you showed.