Can I set search_path when importing a shape file with ogr2ogr from GDAL? - postgresql

I'm importing shape files into Postgresql via this command:
ogr2ogr PG:host=localhost dbname=someDbName user=someUserName password=somePassword shapeFile.shp -nln alternateLayerName -nlt someValidGeometry
This works well but goes into public schema in Postgresql. I'd like to choose a different schema. Is there a way to achieve this with ogr2ogr alone?
There's no reference in man ogr2ogr to schema. My web search wasn't fruitful either.
I know I can do an ALTER TABLE some_table set schema a_different_schema, but that would mean adding another step to the process.
$ ogr2ogr --version
GDAL 2.1.0, released 2016/04/25

After a better web search I found on GDAL pg driver documentation that one can use ACTIVE_SCHEMA=string: Active schema. to set the schema where the table will be created.
I tried it like this:
ogr2ogr -f "PostgreSQL" PG:"dbname=mydb active_schema=layers" country.shp -nln test_table -nlt MULTILINESTRING
And it complains with:
ERROR 1: PQconnectdb failed: invalid connection option "active_schema"
But table gets created and populated properly. So I guess it's ok.

I found a better solution by using lco option:
ogr2ogr -f PostgreSQL "PG:host=localhost port=5432 dbname=some_db
user=postgres password=" someShapeFile.shp -nln desiredTableName
-nlt someValidGeometry -lco SCHEMA=desiredPostgresqlSchema

Related

PostGIS: function ST_AsRaster does not exist. Even using examples from the docs

I'm trying to convert geometries to images, and the functions to do so don't seem to exist.
The following example is from the ST_AsRaster Docs WHich specify the requirements are Availability: 2.0.0 - requires GDAL >= 1.6.0.
SELECT ST_AsPNG(ST_AsRaster(ST_Buffer(ST_Point(1,5),10),150, 150));
This results in:
ERROR: function st_asraster(geometry, integer, integer) does not exist
LINE 1: SELECT ST_AsPNG(ST_AsRaster(ST_Buffer(ST_Point(1,5),10),150,...
I found some info that points towards needing GDAL drivers, however, when I try:
SELECT short_name, long_name FROM ST_GdalDrivers();
I get:
ERROR: function st_gdaldrivers() does not exist
LINE 1: SELECT short_name, long_name FROM ST_GdalDrivers();
I have no idea where to even go to try solving this, why don't the functions exist, was there some config I needed to add, some doc I didn't read?
Even the https://postgis.net/docs/RT_reference.html seems to suggest that it should "just work"?
This is installed from the package manager on Ubuntu 20.0.4.
Version Info SELECT PostGIS_Full_Version();:
POSTGIS="3.0.0 r17983" [EXTENSION]
PGSQL="120"
GEOS="3.8.0-CAPI-1.13.1 "
PROJ="6.3.1"
LIBXML="2.9.4"
LIBJSON="0.13.1"
LIBPROTOBUF="1.3.3"
WAGYU="0.4.3 (Internal)"
You must have forgotten to install the postgis_raster extension:
CREATE EXTENSION postgis_raster;
This extension is new in PostGIS 3.0; before that, its objects were part of the postgis extension.
The documentation mentions that:
Once postgis is installed, it needs to be enabled in each individual database you want to use it in.
psql -d yourdatabase -c "CREATE EXTENSION postgis;"
-- if you built with raster support and want to install it --
psql -d yourdatabase -c "CREATE EXTENSION postgis_raster;"

updating a pgsql table using ogr2ogr, trouble with PK

I have an issue when using ogr2ogr to import new GIS data (from osm or specific), from shp to pgsql.
To create my table, I use:
ogr2ogr -overwrite --config PGSQL_OGR_FID myid -gt 65536 -f "PostgreSQL" "PG:host='host' user='myself' dbname=db password='motdepasse'" -nln rues --config GDAL_DATA "C:/OSGeo4W64/share/gdal" -t_srs EPSG:2154 "C:/test01.shp"
With PGSQL_OGR_FID to force pgsql to use my original column as Primary key. This key will have to make possible my updating.
So, to update with new datas, I would like to do so:
ogr2ogr -update -append --config PGSQL_OGR_FID myid -gt 65536 -f "PostgreSQL" "PG:host='host' user='myself' dbname=db password='motdepasse' active_schema=public" -nln rues --config GDAL_DATA "C:/OSGeo4W64/share/gdal" -t_srs EPSG:2154 "C:/test02.shp"
BUT pgsql doesn't like that at all! I want it to replace data with the same PK by the new one, but it wants to keep the old one and take the new one, which is not possible of course, so the message is:
ERROR: duplicate key value violates unique constraint "rues_pkey"
DETAIL: Key (osm_id)=(92868916) already exists.
Is it possible to use ogr2ogr to replace data? It seems, when updating, wanting to keep everything...
I don't know what to do... Maybe using SQL queries in my request...
Strangely, it seems that this is working with sqlite/spatialite!
(See http://lists.osgeo.org/pipermail/gdal-dev/2011-December/031099.html)
Thanks very much if you have any solution, and sorry for my bad English!
Cyril

Error at importing osm file into a postgres database

I am using osm2psql to import an osm file into a postgres database using the following command:
osm2pgsql -cGs -d osm -S /usr/local/share/osm2pgsql/default.style ~/Downloads/your_file.osm.pbf
When I run it in terminal I get the following error:
Jonathans-MacBook-Pro:Downloads rjth$ osm2pgsql -cGs -d osm -S
/usr/local/share/osm2pgsql/default.style ~/Downloads/vienna-bratislava.osm.pbf
osm2pgsql SVN version 0.84.0 (64bit id space)
Using projection SRS 900913 (Spherical Mercator)
Setting up table: planet_osm_point
NOTICE: table "planet_osm_point" does not exist, skippin
NOTICE: table "planet_osm_point_tmp" does not exist, skipping
SELECT AddGeometryColumn('planet_osm_point', 'way', 900913, 'POINT', 2 );
failed: ERROR: function addgeometrycolumn(unknown, unknown, integer, unknown, integer) does not exist
LINE 1: SELECT AddGeometryColumn('planet_osm_point', 'way', 900913, ...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Error occurred, cleaning up
I have already tried TomH suggested for a similar question and I have followed every step of the official tilemill documentation. for the installation option of postgis I have installed postgresapp.
I was having the same issue.
If create extension postgis was done, then it wasn't with the database osm.
You need to add the extension within the osm database.
rjth$ psql osm
osm=# create extension postgis;
Then run your command to access the osm.pbf file and it should work.

Trouble installing additional module cube in PostgreSQL 8.4

I am trying to use PostgreSQL with the "Seven Databases in Seven Weeks" book. I am using PostgreSQL 8.4.1 on an Ubuntu 10.04 server.
The first task is to create a database named "book" and check if the contrib packages have been installed properly.
$ createdb book
$ psql book -c "SELECT '1'::cube;"
When I do that I get the following output:
ERROR: type "cube" does not exist
LINE 1: SELECT '1'::cube;
I already installed the cube package with the following command:
$ sudo -u postgres psql postgres < /usr/share/postgresql/8.4/contrib/cube.sql
I tried restarting PostgreSQL but the problem persists. When I tried running the package import a second time I got the following message, which explicitly states that type "cube" already exists:
SET
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
ERROR: type "cube" already exists
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
COMMENT
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
ERROR: operator < already exists
ERROR: operator > already exists
ERROR: operator <= already exists
ERROR: operator >= already exists
ERROR: operator && already exists
ERROR: operator = already exists
ERROR: operator <> already exists
ERROR: operator #> already exists
ERROR: operator <# already exists
ERROR: operator # already exists
ERROR: operator ~ already exists
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
ERROR: operator class "cube_ops" for access method "btree" already exists
ERROR: operator class "gist_cube_ops" for access method "gist" already exists
So, what am I doing wrong?
You only installed the extension to your postgres database (the default system database named "postgres") - which is probably not what you want. You need to install the extension to your database - once per database in which to use it.
Or you can install it to a template database (template1 by default, but any database can be used as template) so that every new database created starts out with the functionality pre-installed.
In PostgreSQL 8.4 or older, you need to run in the shell:
psql -d dbname -f SHAREDIR/contrib/cube.sql
Where dbname is the name of your actual target db. Or use the equivalent line that you have in your question.
More info for PostgreSQL 8.4 in the manual here.
Since PostgreSQL 9.1 this has been further simplified and you can just run in a database session:
CREATE extension cube
More in the manual here.
The full command for 9.1 is:
psql -d dbname
CREATE EXTENSION cube;
\q
Where dbname is the name of the database you want to add the extension to.
Note that the last command is a backlash q for quit. And don't forget the semicolon at the end of the second one.

PostgreSQL pgp_sym_encrypt() broken in version 9.1

The following works in PostgreSQL 8.4:
insert into credentials values('demo', pgp_sym_encrypt('password', 'longpassword'));
When I try it in version 9.1 I get this:
ERROR: function pgp_sym_encrypt(unknown, unknown) does not exist LINE
1: insert into credentials values('demo', pgp_sym_encrypt('pass...
^ HINT: No function matches the given name and argument types. You might need to add
explicit type casts.
*** Error ***
ERROR: function pgp_sym_encrypt(unknown, unknown) does not exist SQL
state: 42883 Hint: No function matches the given name and argument
types. You might need to add explicit type casts. Character: 40
If I try some explicit casts like this
insert into credentials values('demo', pgp_sym_encrypt(cast('password' as text), cast('longpassword' as text)))
I get a slightly different error message:
ERROR: function pgp_sym_encrypt(text, text) does not exist
I have pgcrypto installed. Does anyone have pgp_sym_encrypt() working in PostgreSQL 9.1?
On explanation could be that the module was installed into a schema that is not in your search path - or to the wrong database.
Diagnose your problem with this query and report back the output:
SELECT n.nspname, p.proname, pg_catalog.pg_get_function_arguments(p.oid) as params
FROM pg_catalog.pg_proc p
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE p.proname ~~* '%pgp_sym_encrypt%'
AND pg_catalog.pg_function_is_visible(p.oid);
Finds functions in all schemas in your database. Similar to the psql meta-command
\df *pgp_sym_encrypt*
Make sure you install the extension on the desired schema.
sudo -i -u postgres
psql $database
CREATE EXTENSION pgcrypto;
OK, problem solved.
I was creating the pgcrypto extension as the first operation in the script. Then I dropped and added the VGDB database. That's why pgcrypto was there immediately after creating it, but didn't exist when running the sql later in the script or when I opened pgadmin.
This script is meant for setting up new databases and if I had tried it on a new database the create extension would have failed right away.
My bad. Thanks for the help, Erwin.
Just mention de schema where is installed pgcrypto like this:
#ColumnTransformer(forColumn = "TEST",
read = "public.pgp_sym_decrypt(TEST, 'password')",
write = "public.pgp_sym_encrypt(?, 'password')")
#Column(name = "TEST", columnDefinition = "bytea", nullable = false)
private String test;
I ran my (python) script again and the CREATE EXTENSION ran without error. The script also executes this command
psql -d VGDB -U postgres -c "select * from pg_available_extensions order by name"
which includes the following in the result set:
pgcrypto | 1.0 | 1.0 | cryptographic functions
So psql believes that it has installed pgcrypto.
Later in the same script when I execute
psql -d VGDB -U postgres -f sql/Create.Credentials.table.sql
where sql/Create.Credentials.table.sql includes this
insert into credentials values('demo', pgp_sym_encrypt('password', 'longpassword'));
I get this
psql:sql/Create.Credentials.table.sql:31: ERROR: function pgp_sym_encrypt(unknown, unknown) does not exist
LINE 1: insert into credentials values('demo', pgp_sym_encrypt('pass...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
When I open pgadmin it does not show pgcrypto in either the VGDB or postgres databases even though the query above called by psql shows that pgcrypto is installed.
Could there be an issue with needing to commit after using psql to execute the "create extension ..." command? None of my other DDL or SQL statements require a commit when they get executed with psql.
It's starting to look like psql is just flakey. Is there another way to call "create extension pgcrypto" - e.g. with Python's database support classes - or does that have to be run through psql?