Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a basic query on PSQL does not display anything

I have connected to my PSQL database on command line and can run commands like \dt or similar just fine.

However, when I type in a basic query such as SELECT * FROM myTable and hit return, it does not display anything and just goes to the next line waiting for another command.

Any idea why?

like image 413
farjam Avatar asked Oct 13 '15 15:10

farjam


2 Answers

You need to terminate the query with ;

SELECT * FROM myTable;

like image 155
evsheino Avatar answered Sep 20 '22 18:09

evsheino


There is actually a less obvious cause of the above. If you've ommitted a closing bracket ()) somewhere in the query, you'll get the same behaviour. E.g.

SELECT *
    FROM some_table
    WHERE (NOW() - date_value;

It won't attempt to execute the statement until it gets a closing ); before it spits out a syntax error. While the above is fairly obvious where the issue it, it gets a lot trickier to spot with more complex queries. To get out of it, close with multiple brackets ))))))))); which will still resolve and spit out the syntax error so you can continue and correct.

A way to spot if this is the case, is to use the Up arrow through your history - if you're not seeing your previously typed (but not yet executed) statements, then you know you're still in a state where it's trying to close the statement before attempting a syntax check.

The issue isn't limited to brackets, but any other closing symbol such as quotes. The trick is to pay attention to the psql prompt as it shows the parsing state. E.g.;

  • DATABASE=> the => means all is normal and it's parsing a single line
  • DATABASE-> the -> means you're on a new line and likely missing a ;
  • DATABASE(> the (> means you haven't closed a bracket
  • DATABASE'> the '> means you haven't closed a quote.

Etc, you get the idea.

like image 26
DiskJunky Avatar answered Sep 22 '22 18:09

DiskJunky