Making a basic query on PSQL does not display anything - postgresql

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?

You need to terminate the query with ;
SELECT * FROM myTable;

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.

I fixed this exact issue by clicking the maximize button in the top right corner of the query terminal. (PGADMIN4 - PostgreSQL 15)
I'm using PostgreSQL 15 and was running queries. I then maximized the query terminal, and following that, I was no longer able to view the query that was ran successfully. I then clicked the maximize screen and found that the results had simply been hidden behind the maximized query terminal.
Hope this helps!

Related

Incorrect PSQL command puts sql in a state that cannot be exited unless exiting from PSQL completely

As you can see I did not quote Hill correctly thus making the SQL command incorrect. The first SQL line has the database name followed by =# the second line has -#. After the incorrect SQL command there is '#.
The issue is no matter what, I cannot exit the '#.
Only option is to quit psql with \q. Is there a way to go back from '# to =# instead of completely exiting psql?
The '# tells you there is a dangling quote.
To finish the statement enter '); on the '# prompt.
The ' closes the string. The ) ends the VALUES part of the INSERT and the ; ends the whole statement resulting in a syntax error (obviously)
In my hands, you can't even use \q to quit, as that can be a legitimate part of a single-quote quoted string. I just get a message (on Linux) saying Use control-D to quit., which does work.
But to get out of the line entry without closing psql, another thing that you can do is hitting ctrl-C. (But this doesn't work on MSWindows, it bombs all the way out of psql). The problem with that is that you lose your buffer.
If you want to preserve your query buffer so you can go back and edit it to fix the problem, then do as the horse suggests and just close the quote and whatever other open constructs you have, to get the syntax error. Then hit the up arrow to go edit it, adding the missing quote in the proper place and deleting the extra one you had to add at the end to get things to close out. To be really careful, you should make sure the thing you are about to submit really is going to be a syntax error, and not accidentally a valid statement which does something you don't want. When attached to production, I take the ctrl-C route and accept the loss of the query buffer, it is safer.

Prevent Ctrl+Z deleting last line in SSMS after Script As->Alter View

I'm hoping there's just some way to disable this in SQL Server Management Studio (v17.9.1). When I Script As->Alter a view or stored procedure to a new query window, I can hit Ctrl+Z and it deletes the last line of text! I realize as I'm trying to explain this exactly what's happening: Sql is opening a new window and "pasting" the query line by line. Therefore when you undo you're removing the last line and so on.
This becomes a problem when you say, alter a view, make changes and execute, then decide to undo them all so you just hold Ctrl+Z then realize you've erased half your script instead of just going back to the starting point.
Does anyone know how to disable this or an elegant way of preventing it from happening?
This started out as a comment, but it's getting too long, so...
I don't think you can disable ctrl+z, but just like Newton's third law of motion - for every undo there's an equal and opposite redo - so if you've held ctrl+z pressed for too long, you can always restore the lines that ctrl+z deleted simply by using ctrl+y.
Other options are suggested by GSerg in his comments - you can always keep a copy of the original code in a different query window, or even a simple text editor.
One more option is to first save the content of the new query to a file, so you can always reload it's original state if you've messed things up too much.

SSMS - auto complete Parameters (intellisense)

I already got used to intellisense failing (cant really blame it, sql is weird per design) and I know I can "resfresh" it with (CTRL + SHIFT + R) - which sometimes even works.
What makes me mad is the "parameters peek" when I start writing a funktion call. It lists all parameters and highlights (bold) the parameter that is expected at that position.
As soon as something goes "wrong" the "parameter peek" disappears and never shows itself again. (i.e. dbo.SomeFunc(#val1 int , #val2 int) ... start writing "dbo.SomeFunc " and the "peek" will show up. Now insert "-1" instead of "1" and see it fail.)
This drives me crazy, is there a way to bring it up again? (Alt + Space or so?)
I dont know what that "parameter peek" is called and if it is part of intellisense or not. If its not possible to show it again, it would be nice to deactivate without the other intellisense funktions. I tend to rely on it when it pops up and go mad when it disappers again. >:(
SSMS v17.3 (14.0.17199.0)
Thanks in advance for any hint
Jan
Retyping the opening parentheses ( for the parameters does the trick for me in SSMS 2017.6

Powershell commands?

Ok I'm having trouble and google isn't helping, so I thought I'd come to you geniuses. I'm using Powershell and posh-git, and it keeps doing something that I'm sure I can exit out of with a magic command, I just don't know it yet.
Basically, when I run git diff (or something else with a long result), it will only give me a screen's worth of information, and end the screen with a colon
:
And if I keep pressing Enter it will add more to the screen til it is done showing everything for that command, and shows
<END>
But now what? How do I get out of this and back to calling commands? Enter, Esc and the other things I thought to try are not helping. I'm sure this must be a simple thing, but I don't know how to explain to Google what I want.
Anyone know?
if you do a git config -l you may see some relevant entries like:
core.pager='less'
pager.diff=false
pager.log=true
You can enable or disable the pager for different commands, or set a different pager. https://www.kernel.org/pub/software/scm/git/docs/git-config.html has the details, check out the core.pager section and pager.<cmd> sections for specifics.
If you're using 'less' as your pager, hit 'h' at that : prompt to get lots of details about what you can do there, and as pointed out by others, q, Q, or ZZ will get you back to the command line.
You can terminate the current command using CTRL+C. Is that what you're asking?

Oddball issue arising with use of TextWrangler to edit Python 2.7 script which tests multithreading and Queue modules

I'm writing a script in Python 2.7 exploring the use of the multithreading and Queue modules. the script just defines two functions, one of which will be started in a thread instance to fill a Queue, and a second one which will be started in a second thread instance to pull something off the Queue. However, I cannot even get this script to execute, as it chokes on one of the function definitions even before it executes. The code snippet with the problem is:
def listenThread(counter):
while queue.empty() != True:
try:
outcome = queue.get()
print outcome
counter -=1
print counter
except:
return 'queue.get() command loop failing.'
The error message I am getting is:
$ python threading-and-queue-test.py
File "threading-and-queue-test.py", line 34
except:
^
IndentationError: unindent does not match any outer indentation level
$
I've searched SO and checked the docs for correct formation of the try-except construct, and it seems OK. I had earlier versions of the code (without the try-except ) working, so I am getting suspicious about the text editor, TextWrangler v4.0.1.
Can anyone suggest a debug approach or code correction?
Thanks!
Red
As you have already suspected, it is most likely an indentation error. Your text editor is probably mixing the use of tabs and spaces. Replace all tabs with spaces (or vice versa though the standard in python is 4 spaces) and you should see the error disappear.
For those who find this question later like I did it's easy to fix TextWrangler's settings and stop this issue altogether; Go to "Preferences" -> "Editor Defaults" -> "Auto-expand tabs" then set tabs to 4 spaces. Restart TextWrangler for changes to take affect. For existing documents that continue giving you a problem read this.