Postgresql 9.5 Syntax error - postgresql

I perform the same sql scripts on two server with different postgres versions. The first one has postgres 9.4.4 (this works fine) and the other one 9.5 (this throws an exception) installed.
UPDATE archived_invoice SET encrypted_xml
= encrypt(xml::bytea, 'MySuperSecretKey'::bytea, 'aes-ecb/pad:pkcs')
The exception:
Caused by: org.postgresql.util.PSQLException: ERROR: invalid input syntax for type bytea
SQL Status:22P02
Are there any differences between these two postgresql versions?

PostgreSQL will throw an ERROR: invalid input syntax for type bytea at you if the text you're casting into a bytea contains an invalid escape sequence:
# select '\i is not a valid escape sequence'::bytea;
ERROR: invalid input syntax for type bytea
LINE 1: select '\i is not a valid escape sequence'::bytea;
I'm guessing your "xml" column contains or may contain some backslashes. This is fine for XML and text columns, but presents an issue when casting to bytea.
You will need to escape the backslashes:
UPDATE archived_invoice SET encrypted_xml
= encrypt(
replace(xml, '\', '\\')::bytea,
'MySuperSecretKey'::bytea,
'aes-ecb/pad:pkcs'
)

Related

PostgresSQL FuzzySearch

I am trying fuzzy search on PostgresSQL and have PostgreSQL 12.5 version installed.
After enabling extension:
CREATE EXTENSION pg_trgm with schema dev;
I am trying to execute below SQL
select * from dev.hello_world a where 'Hello' % ANY(STRING_TO_ARRAY(a.name, ' '))
SQL Error [42883]: ERROR: operator does not exist: unknown % text
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
I have tried this before and used to work. Not able to get reference from document for change.

How to merge from SpatialLite to PostGIS?

How can I merge a SpatiaLite database to a PostGIS database?
I tried to use pgloader with the following command:
pgloader db.sqlite3 postgresql:///mydb
But it is not working. (I guess it is not supported). Please see the error output:
KABOOM!
FATAL error: Could not prepare an sqlite statement.
Code ERROR: no such module: VirtualSpatialIndex.
Database: /tmp/db.sqlite3
SQL: PRAGMA table_info(`SpatialIndex`)
An unhandled error condition has been signalled:
Could not prepare an sqlite statement.
Code ERROR: no such module: VirtualSpatialIndex.
Database: /tmp/db.sqlite3
SQL: PRAGMA table_info(`SpatialIndex`)
FATAL: Failed to start the monitor thread.
error opening #P"/tmp/pgloader/pgloader.log": Permission denied
I use in SpatialLite only the simple POINT field. Nothing else from SpatialLite. (With Lat/Lng values)
EDIT1:
#Corion
If I try to do your way I get no error doing this:
pgloader --before load_spatialite.sql db.sqlite3
But loading it to PostGIS with this command:
pgloader --before load_spatialite.sql db.sqlite3 postgresql:///mydb
gives me the following error message:
2018-10-10T11:29:16.056000Z ERROR Database error 42883: function load_extension(unknown) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT load_extension('mod_spatialite')
KABOOM!
FATAL error: Database error 42883: function load_extension(unknown) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT load_extension('mod_spatialite')
An unhandled error condition has been signalled:
Database error 42883: function load_extension(unknown) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT load_extension('mod_spatialite')
What I am doing here?
Database error 42883: function load_extension(unknown) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT load_extension('mod_spatialite')
What is here the problem?
SpatiaLite is an extension built into SQLite. You will need an SQLite binary / library that is built with SpatiaLite linked statically or load the SpatiaLite dynamic extension (see link) while importing.
From staring at the pgloader manpage, it seems that you can prepend SQL commands to load the SpatiaLite extension into SQLite like this:
pgloader --before load_spatialite.sql /tmp/db.sqlite3
and load_spatialite.sql contains
SELECT load_extension('mod_spatialite');
You may or may not need to set up additional environment variables such that dynamic libraries are found for your process.

Insert text with single quotes in PostgreSQL 9.4.3

Using this:
Insert text with single quotes in PostgreSQL
I run on Postgres version 9.4.3 with command sql after
insert into test values (1,$$user's log$$);
Postgres returns error message
ERROR: 42601: Syntax error at or near "s"

IBM DB2 drop type error

I have a DB2 Db with this
create type mycar as(<br/>
description varchar(30)<br/>
)MODE DB2SQL;
how to delete that type?
I try drop type mycar, but have an error: DB2 for Linux, UNIX, and Windows: "DECLARE" was expected after ";".
Are you sure you have the ';' as command separator?
The Declare is just after the ';' which seems that your command separator is another, probably #.
For more information about dropping types:
Drop - http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000945.html
Drop types - http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.admin.structypes.doc/doc/t0006611.html

Error on using copy Command in Postgres (ERROR: invalid input syntax for type date: "")

I have a CSV file from which I am trying to use Postgres COPY command in order to populate a table from that CSV file. One of the table columns NEXT_VISIT is of a date data_type. Some of the corresponding fields in the CSV file which are supposed to go into this date column have null values.
The Copy command am running is like so:
COPY "VISIT_STAGING_TABLE" from E'C:\\Users\\Sir Codealot\\Desktop\\rufijihdss-2007-2010\\rufijihdss\\VISIT_TEST.CSV' CSV HEADER
When I run this command I get the error:
ERROR: invalid input syntax for type date: ""
CONTEXT: COPY VISIT_STAGING_TABLE, line 2, column NEXT_VISIT: ""
********** Error **********
ERROR: invalid input syntax for type date: ""
SQL state: 22007
Context: COPY VISIT_STAGING_TABLE, line 2, column NEXT_VISIT: ""
How can I run the copy command and get Postgres to accept that some of the fields in the CSV file corresponding to NEXT_VISIT have values ""?
Add WITH NULL AS '' to your command (COPY expects NULLs to be represented as "\N" (backslash-N) by default).
COPY "VISIT_STAGING_TABLE" from E'C:\\Users\\Sir Codealot\\Desktop\\rufijihdss-2007-2010\\rufijihdss\\VISIT_TEST.CSV' WITH CSV HEADER NULL AS ''
More details here: postgresql COPY
I was having the exact same problem, and what solved it for me was to use the statement WITH NULL ''. It is important not to have a space between the apostrophes.
I originally used the statement WITH NULL ' ' and got the same error message you did (ERROR: syntax error at or near "WITH NULL").
But when I eliminated the space between the apostrophes it worked.