While trying to run PL/pgSQL via JDBC driver (postgresql-9.4.1211.jre7.jar; using ANT) I get following error: ERROR: syntax error at or near "$"
Is there any way howto fix this via setting JDBC properties or changing the query of PL/pgSQL?
query:
DO $$
BEGIN
CREATE SEQUENCE id_sequence_SEQ OWNED BY id_sequence.id;
EXCEPTION WHEN duplicate_table
THEN
END
$$
LANGUAGE plpgsql;
error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$"
Position: 5
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2458)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2158)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:291)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:432)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:358)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:305)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:291)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:269)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:265)
I tried executing a DO statement with JDBC and it works just fine.
There must be something weird in your statement. Try and log the statement by setting log_min_error_statement and log_min_messages to error. Execute the statement, then look at the message in the PostgreSQL log file. It should contain the statement that was actually executed. Examine it for weird characters and other strangeness.
Please also note that there is a syntax error in you PL/pgSQL: there must be something between THEN and END. For a no-op, use the statement NULL;.
Related
I am trying to run a for loop via psql command in Linux to do some spatial operations in PostGIS database
My SQL command which I successfully ran in dbeaver looks something like this
DO $$
declare
the_name varchar(50);
BEGIN
FOR the_name IN
SELECT "name" FROM myschema.mytable WHERE "idcolumn" = 'selected id' LIMIT 4
LOOP
some operations inside the loop...
END LOOP;
RAISE NOTICE 'the name - (%)', the_name;
END;
$$;
I usually copy-paste the dbeaver SQL command inside psql -c and it runs fine. But I am getting a syntax error when using the above code. I am a beginner in SQL and any help is appreciated. Thanks.
Edit
This is the error message - it's weird because I don't have this number in the code. It's probably reading from memory or something.
ERROR: syntax error at or near "377643"
LINE 1: DO 377643 declare the_name varchar(50); BEGIN FOR the_...
I have the following code:
DROP TABLE IF EXISTS pltest;
CREATE TABLE pltest (x jsonb);
DO $$
DECLARE startdate text := to_char(current_date - 1, 'YYYYMMDD');
BEGIN
EXECUTE format(
'COPY pltest FROM PROGRAM ''curl "https://example.com/events/start_date=%sT000000Z"''',
startdate
);
END
$$ ;
It basically imports the URL with a parameterized COPY statement so it always imports the data of the last 24 hours, it runs perfectly in SQL shell, but when i tried adding a pgagent job with that code, pgagent returns "syntax error near DECLARE".
PostgreSQL version: 13.3
PgAgent version: 13
This code looks correct. It fails with expected error.
ERROR: invalid input syntax for type json
DETAIL: Token "<" is invalid.
CONTEXT: JSON data, line 1: <...
COPY pltest, line 1, column x: "<!doctype html>"
SQL statement "COPY pltest FROM PROGRAM 'curl "https://example.com/events /start_date=20210526T000000Z"'"
PL/pgSQL function inline_code_block line 5 at EXECUTE
Maybe your client breaks source code, or maybe your Postgres is too old.
PostgreSQL syntax problem.
Query below will run but result is:
ERROR: out of memory for query result
SELECT
AD.ADDRESS_DETAIL_PID as ADDRESS_DETAIL_PID,
AD.STREET_LOCALITY_PID as STREET_LOCALITY_PID,
AD.LOCALITY_PID as LOCALITY_PID,
AD.BUILDING_NAME as BUILDING_NAME
When the query is amended to:
set FETCH_COUNT=1000
SELECT
AD.ADDRESS_DETAIL_PID as ADDRESS_DETAIL_PID,
AD.STREET_LOCALITY_PID as STREET_LOCALITY_PID,
AD.LOCALITY_PID as LOCALITY_PID,
AD.BUILDING_NAME as BUILDING_NAME
the result is:
ERROR: syntax error at or near "SELECT" LINE 3: SELECT
^ SQL state: 42601 Character: 23
except that when a colon ; is included at the end the rssult is:
ERROR: unrecognized configuration parameter "fetch_count" SQL state:
42704
I am seeking help with the syntax.
FETCH_COUNT is a psql interval variable and is not executed on the server.
SET is a SQL command that's executed on the server and cannot be used for setting psql internal variables. use \set instead.
\set FETCH_COUNT 1000
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.
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"