PostgreSQL syntax error at or near "$1" - postgresql

sql"""copy updateTable
from $path
credentials 'aws_access_key_id=<my_access_key_id>;aws_secret_access_key=<my_secret_access_key>'
json '<path_to_s3_repository>'
gzip;""".update().apply()
Above command gives
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
when run on spark streaming using scala. What might be the issue? The resultant query runs fine when run from command line.

You cannot use a parameter with COPY.
You have to add the literal value of $path to the statement string and execute that.

Related

Powershell : Unexpected token '-l' in expression or statement

im trying to use the following command to convert files from one format to another using a tool, but powershell gives me an error:
"C:\Program Files\Java\jre1.8.0_361\java" -cp C:\Users\yporn\Desktop\work\travel40\public\doc\weka.jar weka.clusterers.SimpleKMeans -T C:\Users\yporn\Desktop\work\travel40\public\doc\unseen.arff -l C:\Users\yporn\Desktop\work\travel40\public\doc\Tourist.model
The error is
Unexpected token '-cp' in expression or statement.

Problem with postgres \copy command for export query resullt to file

I tried to export a select query result to a csv file. I used Postgres \copy metacommand and command line (psql) to do it. But I got a Syntax error and can't understand why. The Query looks fine to me. Maybe the reason for using metacommand instead of COPY?
The query
\copy
(
SELECT geo_name, state_us_abbreviation, housing_unit_count_100_percent
FROM us_counties_2010
ORDER BY housing_unit_count_100_percent DESC
LIMIT 20
)
TO '/username/Desktop/us_counties_2010_export.csv'
WITH(FORMAT CSV, HEADER, DELIMITER '|');
Error message
ERROR: syntax error at or near "TO"
LINE 7: TO '/username/Desktop/us_counties_2010_export.csv'
\copy is a metacommand given to psql, not a regular command sent to the server. So like other metacommands, the entire \copy command must all be given on one line and doesn't end in a ; but rather a newline.
If you look closely, you will see the first error you got was \copy: arguments required

jq reading filter from a file results in Invalid Syntax Error

filter.jq
".security = {hideVersionStringsWhenNotLogged: true}"
When trying to apply the filter,
jq --from-file filter.jq a.json
encountering the following error
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at , line 1
tried all combinations of single and double quotes to no avail.
Any suggestions?
Thx
If you want the filter to be read from a file, do not enclose it in the quotation marks that are needed if the filter is specified on the command line.
So the contents of your filter.jq file could look like this:
.security = {hideVersionStringsWhenNotLogged: true}

Postgres \copy throws a syntax error at STDOUT when exporting as CSV (v8.0.2)

I'm trying to locally download the results of a query as csv on a postgres instance that I have read-only access to (v8.0.2). I've truly read 10 different ways of going about this and have tried implementing them all (here, here, here, here, and here), but every single time I try to execute the copy command, I get the following error:
ERROR: syntax error at or near "STDOUT"
Here are five of the roughly 20 permutations I have tried. The foo_bar table is a temporary table that was created as the output of a query.
=> \copy "pg_temp_5.foo_bar" TO '/Users/baz/Downloads/foo_bar.csv' DELIMITER ',' CSV
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY "pg_temp_5.foo_bar" TO STDOUT DELIMITER ',' CSV
=> \copy "pg_temp_5.foo_bar" TO '/Users/baz/Downloads/foo_bar.csv' CSV
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY "pg_temp_5.foo_bar" TO STDOUT CSV
=> \copy "pg_temp_5.foo_bar" TO '/Users/baz/Downloads/foo_bar.csv' WITH FORMAT "csv"
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY "pg_temp_5.foo_bar" TO STDOUT WITH FORMAT "csv"
=> \copy pg_temp_5.foo_bar TO '/Users/baz/Downloads/foo_bar.csv' With CSV
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY pg_temp_5.foo_bar TO STDOUT With CSV
=> \copy foo_bar TO foo_bar.csv With CSV
ERROR: syntax error at or near "STDOUT"
LINE 1: COPY foo_bar TO STDOUT With CSV
I figure that it isn't a permissions issue, as if it were, I would be thrown a "permission denied" when I try to run the command. I also know that there have been similar issues with the \copy command as documented by postgres here, but nothing has been noted specifically with regards to my case. Any help would be greatly appreciated!

A simple COPY statement in PostgreSQL 8.4

I am trying to copy a table's data to a file. I have a small database table in the local windows machine and this code has no problems. When I use this in the development environment (still windows, different database), I get an error.
My standard_conforming_strings is currently off.
This query:
COPY some_table TO 'C:\\temp\\test.csv' WITH CSV HEADER;
Gives this error:
WARNING: nonstandard use of \\ in a string literal
LINE 1: COPY t_table TO 'C:\\temp\\test.csv' WITH CSV HEADER;
^
HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
ERROR: relative path not allowed for COPY to file
********** Error **********
ERROR: relative path not allowed for COPY to file
SQL state: 42602
I have tried:
'C:\temp\test.csv'
'C:\\temp\\test.csv'
'C:/temp/test.csv'
'C:\/temp\/test.csv'
And in all my test, the caret ^ in the error message points at ^'C:.
You are using PostgreSQL on linux not Windows. Just using a Windows client.
COPY is a server side command. It expects a path on the server. That file is on your Windows client PC so the server cannot access it.
Use \copy from psql instead. Or use PgAdmin's CSV importer.
(Newer PostgreSQL versions would give you a HINT about this in the error message.)