When I run an SQL script using psql.exe I am not getting error codes when an error occurs. Is there any way to get the psql error codes?
I tried setting VERBOSITY to 'verbose' like this, but no use:
\set VERBOSITY 'verbose'
I am using psql of version psql (PostgreSQL) 8.4.2.
Get rid of the quotes:
test=# \set VERBOSITY verbose
test=# select broken;
ERROR: 42703: column "broken" does not exist
LINE 1: select broken;
^
LOCATION: transformColumnRef, parse_expr.c:766
Related
I've created a SQL file I run through the psql command that roughly looks like as follows:
truncate table my_table;
\set content `cat /workdir/test.json` insert into my_table values ('test_row', :'content');
The first line is somewhat irrelevant to the problem, except for the fact it does print out "TRUNCATE TABLE", so it is reading and running the SQL file correctly, at least initially. However, the insert row is never created, the table is always empty. Yet no error message pops up.
The JSON file has a valid value (even if I pare it down to super basic {}). I've also tried passing the sql command directly (just to cover my bases, tried it with just one '' and same, with three it gives invalid command error):
psql [...] -c "\\set content `cat /workdir/test.json` insert into my_table values ('test_row', :'content')"
Again, no output message, no new rows created. However not using the meta-command \set does work. E.g.:
psql [...] -c "insert into my_table values ('test_row', '{}')"
Seems like there's something it doesn't like about the meta-command \set, but without any error info, not sure what I'm doing wrong.
Both the script and database are running on the same VM. That is, script can call host via 'localhost' and the filesystem/filepaths should be the same, I think, should that matter.
A psql meta-command (something that starts with a backslash) are terminated by the end of line; you cannot have an SQL statement on the same line.
Write the \set in one line and the INSERT in another.
If you want to use the -c option of psql, use several -c options:
psql -c "\\set ..." -c "INSERT ..."
I use psql to connect to the PostgreSQL database on terminal. If I run a simple select query,
select count(*) from my_schema.my_table;
I get the error:
ERROR: syntax error at or near "clear"
LINE 1: clear
UPDATE:
I cannot even execute a simple select query like select * from my_schema.my_table; or select * from my_table;. It gives me the error:
ERROR: relation "my_table" does not exist
LINE 1: select * from my_table;
Did you login into right database.
The error 'relation "my_table" does not exist' seems cause by wrong database selection.
For example:
psql -h localhost -p 5435 -U myuser -W <>
all I am trying to restore my db from pg_dump that I got from my server. I know there are plenty similar question, I tried what was suggested but still can not resolve my issue.
I created my pg_dump via this command:
pg_dump name_of_database > name_of_backup_file
Then I create new db:
createdb -T template0 restored_database
Then I restore using this command:
psql -1 -f name_of_backup_file.sql restored_database
It runs and then I got different syntax errors, for example:
psql:nurate_pg_dump.sql:505: invalid command \nWatch
Query buffer reset (cleared).
psql:nurate_pg_dump.sql:512: invalid command \nThe
psql:nurate_pg_dump.sql:513: invalid command \N
psql:nurate_pg_dump.sql:4098: ERROR: syntax error at or near "9"
LINE 1: 9 the course is not difficult, but Zauresh Atakhanova is not...
I believe since I made pg_dump from the same server, and try to restore it there nothing changed in my server setup, or version of postgres, so I think my db should restore correctly. How can I resolve these syntax errors?
EDIT: Ok, the problem is not with the syntax:
CREATE EXTENSION
ERROR: must be owner of extension plpgsql
This error is thrown at this line:
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
try to restore your database with
psql restored_database < name_of_backup_file
I am using psql command to connect and issue a query on postgreSQL database. Can anybody let me know how to check the return status of the executed query in shell script.
I have used echo $? command to check the status but it always returning zero.
Thanks for the help.
psql return code is documented as:
EXIT STATUS
psql returns 0 to the shell if it finished normally, 1 if a fatal error
of its own occurs (e.g. out of memory, file not found), 2 if the
connection to the server went bad and the session was not interactive,
and 3 if an error occurred in a script and the variable ON_ERROR_STOP
was set.
You probably just want to use ON_ERROR_STOP.
Failure getting tested and reported to the shell:
$ psql -d test -v "ON_ERROR_STOP=1" <<EOF
select error;
select 'OK';
EOF
ERROR: column "error" does not exist
LINE 1: select error;
$ echo $?
3
Failure getting ignored and not reported to the shell:
$ psql -d test <<EOF
select error;
select 'OK';
EOF
ERROR: column "error" does not exist
LINE 1: select error;
^
?column?
----------
OK
(1 row)
$ echo $?
0
As mentioned here, you can also add this line at the top of your SQL file/script:
\set ON_ERROR_STOP true
I get a little confused some times when working with psql between when to use a set vs. \set vs. \pset. I think that:
set is for session variables on my connection to the db. For example SET ROLE dba;
\set is for local variables for this psql session. For example \set time 'select current_timestamp'
\pset is for psql settings for this psql session. For example '\pset border 2'
But, I've never found what I thought was a good explanation of each. Are my assumptions above correct?
I'm using PostgreSQL 9.4
Basically correct. The important difference is that SET is an SQL command while the other two are psql meta-commands - indicated by the leading \.
SET is an SQL command to change run-time parameters. It is executed on the server and has nothing to do with psql per se.
\set is a psql meta-command:
Sets the psql variable name to value [...]
Note: This command is unrelated to the SQL command SET.
\pset is another psql meta-command:
This command sets options affecting the output of query result tables