psql extra command line argument - postgresql

Can someone tell me why I get the error extra command line argument here? When I use -f and give it the full path to the sql file it works fine. I would like to use a relative path instead so I was trying to use the \ir command.
psql -c \c postgresql://docker:1234/nbt?ssl=true -U admin -v username='user73291' -v recipeId=2 -c \ir '../../../resources/sql/myfile.sql'
Error:
psql: warning: extra command-line argument "../../../resources/sql/myfile.sql" ignored
ERROR: syntax error at or near "ir"
LINE 1: ir
^
Thanks!

\i is a meta-command to be used in the psql command line, not in the shell command line. What is the problem with -f?

Related

vagrant postgreSQL run COPY

I want to COPY rows from a CSV file (From my Mac locally) into my postgreSQL DB, which is running on Vagrant.
As far as I know the COPY command can only be runned as super user.
This would be the command which fits:
COPY part_model FROM '/Users/myusername/Desktop/csv-file-to-import.csv' csv header;
Then running from my Terminal in the project folder I also have my Vagrantfile and I usually create the vagrant machine, I tried to run this command:
vagrant ssh -c "sudo su - postgres -c 'psql -d databasename name -U username -h localhost COPY part_model FROM '/Users/myusername/Desktop/part_model-import.csv' csv header'";
But I get this error/warning:
psql: warning: extra command-line argument "COPY" ignored
psql: warning: extra command-line argument "part_model" ignored
psql: warning: extra command-line argument "FROM" ignored
psql: warning: extra command-line argument "/Users/antonhorl3/Desktop/part_model- Import.csv" ignored
psql: warning: extra command-line argument "csv" ignored
psql: warning: extra command-line argument "header" ignored
Any help appreciated! Thanks!

syntax error at or near ":" when running parametrized query from shell

I'm trying to run a parameterized query from shell.
But when I run:
p='some stuff'
psql -d "dbname" -v v1="$p" -c "SELECT * FROM table WHERE name=:'v1'"
I'm getting the following error:
ERROR: syntax error at or near ":"
Meanwhile:
psql -d "dbname" -v v1="$p" -c "\echo :'v1'"
works normally. (returns as expected: 'some stuff')
You cannot use the variable defined in -v in -c command (see below). Try passing the command into the standard input:
psql -d "dbname" -v v1="$p" <<< "SELECT * FROM table WHERE name=:'v1'"
From the document:
-c command
--command command
...
command must be either a command string that is completely parsable by
the server (i.e., it contains no psql-specific features), or a single
backslash command.
...
-v does set the psql's internal variable, which is psql-specific features. That's why you got the syntax error.

How to add Postgres extensions via .sh file

I'm trying to add extensions for Postgres by writing the following script (setup.sh):
sudo -u postgres psql my_database
CREATE EXTENSION adminpack;
when I do vagrant up, it supposed to run and add extensions automatically by running the script. However, I got the error message that
==> default: ERROR: syntax error at or near "exit"
==> default: LINE 1: exit
==> default: ^
==> default: /tmp/vagrant-shell: line 24: CREATE: command not found
Please note that I have installed all the necessary postgres stuff to run the code above. In addition, when I enter these command manually, it successfully creates the extension. Thanks to whoever that helps.
try:
sudo -u postgres psql my_database -c "CREATE EXTENSION adminpack"
https://www.postgresql.org/docs/current/static/app-psql.html
-c command
--command=command
Specifies that psql is to execute the given command string, command.
also consider using -f sql_file.sql for more complicated scripts, or smth like:
psql <<EOF
\x
SELECT NOW();
SELECT * FROM foo;
EOF

Postgres from unix shell out put not appear in Log

I am writing a batch job for Postgres for first time. I have return ".sh" file, which has a command with out any out put in the log or console.
Code
export PGPASSWORD=<password>
psql -h <host> -p <port> -U <user> -d <database> --file cleardata.sql > log\cleardata.log 2>&1
What I did at cammond line
su postgres
and run ./cleardatasetup.sh
Nothing is happening.
Please note : When I try psql command in Unix command line, I am getting message as some SQL exception which is valid.
Can any one please help me in this regard.
You probably wanted to create log/cleardata.log but you have a backslash where you need a slash. You will find that the result is a file named log\cleardata.log instead.
The backslash is just a regular character in the file's name, but it's special to the shell, so you'll need to quote or escape it to (unambiguously) manipulate it from the shell;
ls -l log\\cleardata.log # escaped
mv 'log\cleardata.log' log/cleardata.log # quoted

psql command-line variable interpolation leads to syntax error

From the psql command documentation, the --variable command described as making variables available for substitution with :name syntax. However,
psql --variable=var="'hello'" -c 'select :var'
...results in a syntax error:
ERROR: syntax error at or near ":"
LINE 1: select :var
This works correctly if the query text is fed in on stdin in bash:
psql --variable=var="'hello'" <<<"select :var"
Or by using POSIX sh:
psql --variable=var="'hello'" <<<'EOF'
select :var
EOF