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.
Related
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?
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?
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
I'm trying to experiment on my MySQL server for the first time running from MAMP server. Here's a screenshot of what I put into the command line and how it responded: https://i.imgur.com/BGd0G8Z.png
Each new "->" is what happens when I hit enter. Thank you!
The -> prompt is for line continuation. It is meant to continue writing your query on multiple lines. Adding a ; terminator would let mysql know that it should execute the query, which will fail because the multiline query in the screenshot is invalid and you will return to the mysql> prompt.
How can I customize the prompt in the PostgreSQL command line tool psql (ideally in a per-user start-up script)?
In particular, I'd like to be able to change it while still including the character that indicates whether the command is multi-line (eg. =, -, ', etc.).
I'm running Ubuntu 10.04 (Lucid), PostgreSQL 8.4.4.
You can certainly customize the prompt.
From the documentation:
The prompts psql issues can be
customized to your preference. The
three variables PROMPT1, PROMPT2, and
PROMPT3 contain strings and special
escape sequences that describe the
appearance of the prompt. Prompt 1 is
the normal prompt that is issued when
psql requests a new command. Prompt 2
is issued when more input is expected
during command input because the
command was not terminated with a
semicolon or a quote was not closed.
Prompt 3 is issued when you run an SQL COPY command and you are expected to
type in the row values on the
terminal.
If you want to set the prompt on a per user basis, you can add the \set commands to the user's .psqlrc file.
So, your $HOME/.psqlrc would be something like this:
\set PROMPT1 '(%n#%M:%>) %`date +%H:%M:%S` [%/] \n%x%# '