psql prompt changes to "-#" and the statement isn't committed - postgresql

I'm new to Postgres and I set up a database and table. On the Ubuntu 18.04 command line (on a cloud server) I issued the following command using psql:
INSERT INTO psq01(date, email, firstname, lastname, password, comments)
VALUES ("052419", "mjs#outlook.com", "John", "Smith", "blank", "No comment")
After I issue that command, the psql command prompt switches from =# to -#, which means the transaction is not finished, so I issued COMMIT, but the prompt still shows as -#.
My question is: what do I do after an insert into command? Why am I getting the -# prompt?

The dbname-# prompt means that psql is waiting for a continuation line because you haven't terminated the statement with a semiculon (;) yet.
Either type a single semicolon and Enter to finish the command or type Ctrl+C to interrupt it and start over.
Are you sure that you want to use a superuser for inserting into a table?

Related

SQL Shell (psql) don't run any commands on Windows [duplicate]

I'm new to Postgres and I set up a database and table. On the Ubuntu 18.04 command line (on a cloud server) I issued the following command using psql:
INSERT INTO psq01(date, email, firstname, lastname, password, comments)
VALUES ("052419", "mjs#outlook.com", "John", "Smith", "blank", "No comment")
After I issue that command, the psql command prompt switches from =# to -#, which means the transaction is not finished, so I issued COMMIT, but the prompt still shows as -#.
My question is: what do I do after an insert into command? Why am I getting the -# prompt?
The dbname-# prompt means that psql is waiting for a continuation line because you haven't terminated the statement with a semiculon (;) yet.
Either type a single semicolon and Enter to finish the command or type Ctrl+C to interrupt it and start over.
Are you sure that you want to use a superuser for inserting into a table?

Ending Postgres Query without leaving the command line - Get back to command line automatically

Whenever I run a Postgres query it appears that you have to completely quit out of the command line.
I have seen it done where you can press CTRL-C and you are taken back to the PSQL command line i.e., databasename=>. Additionally, if I am in the middle of viewing results and I press CTRL-C, how can I have Postgres send me back to databasename=>?
Bonus:
Is there a way to script is so if I type usedb databasename folllwed by psql, Postgres will know which database I am referring to and automatically connect me to it so I dont have to type \connect databasename ?
Once a postgres query has run and has returned its table of results in the 'psql' command line environment it should drop you back in to the same database that you ran the previous command from.
If you want to connect directly to a database from your terminal :
psql -d nameofdatabase
If you want to connect using a script you can access postgres by url :
postgres://username:password#localhost/nameofdatabase
where 'localhost' could be replaced by the ip of the database you are trying to connect to if its not on the same machine.
Instead of pressing Ctrl-C, press Ctrl-D. In Unix, Ctrl-D is the End-of-File (EOF) character. That is what will make psql quit---just like if you fed it a script on stdin and it got to the end. It works in many other REPLs too, like irb, rails console, python, R, bash, etc.
The reason Ctrl-C doesn't exit is so that you can use it to abort an individual command, e.g. a long-running query.
EDIT: Also, if you are viewing results and they are paged (they appear on a new screen and you can scroll up and down), you can get back to the psql prompt by typing q. That's because by default the pager used is just less. You can say man less to read more about it. Or experiment with it on any text file: less /etc/services.
Personally I find paging in psql annoying, so I turn it off by creating a file named ~/.psqlrc with this line:
\pset pager
(Also, sorry if you know this already: ~ is just an abbreviation for "my home directory". So ~/.psqlrc is the same as /home/whatever/.psqlrc.)
Bonus: If you want to connect to a specific database, you can say psql -d foo or even just psql foo.
just enter \q or q in the psql terminal

How do I execute a query automatically in PostgreSQL when connecting via shell?

I want to be able to execute a statement automatically when I connect to Postgres with psql and then remain connected so I can type in further commands.
Currently, every time I connect, the first thing I do is type:
SET search_path = 'something';
Or maybe I would want to do something else like:
SELECT COUNT(*) FROM sometable;
I know there is a -c argument to psql that will execute a command and then exit. I'm looking for a way I can execute a command upon connecting and then remain in the client.
(Note: I prefer not to alter the database, schema or role to make the search_path permanent, as all of the solutions I have found seem to dictate. I want to set it every time I connect. But again, this question could apply any SQL statement.)
I have tried this:
echo "SET search_path TO 'mything'" | psql
but that behaves the same way as:
psql -c "SET search_path TO 'mything'"
Is what I'm asking for doable?
psql will look for, and execute any commands found in, a couple of places every time it starts up. One is the system-wide psqlrc file, and one is in the home-directory of the login that's running psql, ~/.psqlrc.
So, you could add the SET command that you always want to be run, to your .psqlrc file, and it'll get executed every time you start up. See the example below:
~ $ cat ~/.psqlrc
SET search_path='mything';
~ $ psql
SET search_path='mything';
SET
psql (8.4.20, server 9.2.10)
WARNING: psql version 8.4, server version 9.2.
Some psql features might not work.
Type "help" for help.
rhnschema=# show search_path;
search_path
-------------
mything
(1 row)
rhnschema=#

Meaning of different command line prompts in PostgreSQL?

I have the following code from my command line...
psql (9.1.10)
Type "help" for help.
postgres=# CREATE DATABASE exampledb
postgres-#
After I entered the CREATE DB command the prompt changed from ending with "=#" to "-#". I would like to know what this change means and what the implications are for receiving and processing commands.
I now realize that the "postgres=#" prompt is a fresh prompt waiting for the start of a new command, while the "postgres-#" is the result of hitting enter after typing a command that does not end with a semicolon.
The semicolon denotes the end of a command, so pressing enter without a terminating ";" suggests to postgres that you would like to continue writing your command on a new line.
Inserting a semicolon at any point and pressing enter will return you to the original prompt.

how to run db2 sql command from a command line?

how can I run
sql command UPDATE CONTACT
SET EMAIL_ADDRESS = 'mytestaccount#gmail.com'
via command line
for db2 database
on linux
from a shell script file?
You need to be logged into your linux machine with an id that has "db2profile" in it's .profile.
If you are not sure what I'm talking about, look at the .profile of the db2 instance owner (usually db2inst1 unless you changed it during the install). The simplest thing would probably be to log in as the db2 instance owner.
Once you are logged in, type "db2" at the command line. If "db2" is not found, then recheck the .profile stuff.
To execute your sql command type the following at the command line (substitute with the name of the database you want to connect to):
db2 connect to <database name>
db2 UPDATE CONTACT SET EMAIL_ADDRESS = 'mytestaccount#gmail.com'