Go back up a line in psql - 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
;

Related

PSQL does not display query output

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.

How to hide ACCEPT command used for user input in SQL Plus while spooling it in a text file? I have tried "set VERIFY off"

I am new in SQL Plus. I have multiple queries in a SQL file.
Some queries have variables so whenever I use ACCEPT for that, the output file gets spooled including the command and input I entered.
Something like that:
SP2-0003: Ill-formed ACCEPT command starting as ,name char prompt 'Enter name:'
with the old and new statements like:
Enter value for name: 'john diaz'
old 3: (select * from sample where upper(name) = upper(&name)),
new 3: (select * from sample where upper(name) = upper('john diaz'))
How to remove those statements from output file?? Anyone??
SQL> SET VERIFY OFF
SQL> ACCEPT name CHAR PROMPT 'Enter name:: ' HIDE
Enter name:
SQL> select * from sample where upper(name) = upper(&name);
This way, it will not show OLD/NEW Value, and also you could enter Value in Hidden Way. No display on what you enter.

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.

Resources