PSQL does not display query output - psql

I have connected to my PSQL database from the command line. However, when I run commands like \dt, or any other query for that matter, I simply don't get any output. It simply returns my_database=>
my_database=> \dt
my_database=> \dn
my_database=> select * from table limit 1;
my_database=>
\x does not help. Any ideas?

It must be that you have redirected standard output to a file with the \o filename command. Use \o without an argument to get the output on the console again.

Related

Go back up a line in psql

In psql, I want to go back up a line and edit my SQL from the line above. How would I accomplish this? I tried the up arrow to no avail. My only recourse is to exit psql and jump back in.
For some context:
I want to go back to the line above and change SET dw1 = TRUE
I am ssh'd into the server using gitbash for windows
You haven't finished the command. Notice how the first line ends with =# while the second line ends with -#, which indicates that it's awaiting further text.
You need to end the command with a semicolon (;).
Once you're at a new line that starts with =# you can press up to retrieve the previous command.
Here's an example where I entered select 1<ENTER> followed by ;<ENTER> followed by <UP>:
db=# select 1
db-# ;
?column?
----------
1
(1 row)
db=# select 1
;

How to show data in a table by using psql command line interface?

Is there a way to show all the content inside a table by using psql command line interface?
I can use \list to show all the databases, \d to show all the tables, but how can I show all the data in a table?
Newer versions: (from 8.4 - mentioned in release notes)
TABLE mytablename;
Longer but works on all versions:
SELECT * FROM mytablename;
You may wish to use \x first if it's a wide table, for readability.
For long data:
SELECT * FROM mytable LIMIT 10;
or similar.
For wide data (big rows), in the psql command line client, it's useful to use \x to show the rows in key/value form instead of tabulated, e.g.
\x
SELECT * FROM mytable LIMIT 10;
Note that in all cases the semicolon at the end is important.
Step 1. Check the display mode is "on" by using
\x
Step 2. Don't forget the ;
I tried for fifteen minutes just because I forgot the semicolon.
AND USE UPPERCASE ENGLISH.
TABLE users;
And you will get something like
On Windows use the name of the table in quotes:
TABLE "user"; or SELECT * FROM "user";
you should use quotes
example =>
1) \c mytablename
2) SELECT * FROM "mytablename"; OR TABLE "mytablename";
postgres commande line
to show databases : \l
to show tables : \dt
to show data in table x : SELECT * FROM "x";
to exit : \q
If you use schemas, the following will be correct:
SELECT * FROM "schema-name"."table-name";

SQL querying of a table

I have a ruby on rails application which runs on Postgres database.
(application is not mine by the way, just trying to mess around with and learn more)
So, if I run a simple query such as this:
SELECT * FROM users ORDER BY users.id ASC LIMT 1
I get this:
SyntaxError: unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
SELECT * FROM users ORDER BY users.id ASC LIMT 1
I've been getting that error pretty consistently regardless of any query I run, no matter how simple or complicated.
Why?
EDIT 1
Btw, wanted to mention that this query is run in the console ... fyi.
Even a simple query like this: SELECT * FROM user LIMIT 1 gets me the same error, regardless.
EDIT 2
This is what I get:
psql (9.0.13, server 9.3.1)
WARNING: psql version 9.0, server version 9.3.
Some psql features might not work.
Type "help" for help.
nameofApp=# SELECT * FROM user LIMIT 1
nameofApp-#
nameOfApp-#
nameofApp-#
The hashmarks continue when I press Enter. It doesn't seem to perform the query.
Just connect to Postgresql by psql NAMEOFDATABASEHERE
Then end all your queries with a semicolon.
select * from users; (ENTER)
If you don't type the semicolon the interpreter keeps waiting for more statements and you will be greeted by the next hash sign when pressing enter.
Hope it helps!
You need to run this in the db console. Try rails db from the commandline. Or for an equivalent result in the console try: User.first

psql select statement fails while issuing from command line

Im on the psql command line prompt and when I issue a select statement and I dont see any response..
pv is my database
pv-#select * from named_table
does not give me any result?
is that the right way to do it?
You need to add a ; at the end of your SQL statement to tell psql that you're done and that the query should now be executed.
The query you're given is correct, but you have to give semicolon ; at the end of the query.
Example:
pv-#select * from named_table ;
Actually, If you miss to give semicolon it doesn't show any error, instead of that it will show the secondary prompt.
So if you give semicolon, It will be execute properly.

.group not returning all columns

I have a .group query that is not returning all the columns in the select and I was wondering if someone could validate my syntax.
Here is a query with a .group and the result from my console;
Expense.select('account_number, SUM(credit_amount)').group(:account_number).first
Expense Load (548.8ms) EXEC sp_executesql N'SELECT TOP (1) account_number, SUM(credit_amount) FROM [expenses] GROUP BY account_number'
(36.9ms) SELECT table_name FROM information_schema.views
Even though I select two columns, I'm only getting the first one to return. I'm wondering if I may be dealing with an db adapter problem.
Try giving your sum an alias:
expense = Expense.select('account_number, SUM(credit_amount) AS credit_amount').group(:account_number).first
puts expense.credit_amount
ActiveRecord doesn't create a default alias for aggregation operations such as SUM, COUNT etc... you have to do it explicitly to be able to access the results, as shown above.
The SUM(credit_amount) column from the SQL has no alias and will not have a column name by default. If you change it to have an alias SUM(credit_amount) As 'A' for example and select the alias name, it should pick it up.

Resources