PostgreSQL: How to scroll up in long output - postgresql

Say I get a large query back. Postgres gives me the --More-- indicator. Pressing <space> moves down a page. Pressing <enter> moves down a line. Is there a way to scroll back up? Is it possible to pipe the output to something like less?
I'm accessing PostgreSQL 9.5 on CentOS7 through PuTTY.
For example:
pundb=# \x on
pundb=# select * from pg_roles;
-[ RECORD 1 ]--+-------------
rolname | dinner
rolsuper | t
rolinherit | t
rolcreaterole | t
rolcreatedb | t
rolcanlogin | t
rolreplication | t
rolconnlimit | -1
rolpassword | ********
rolvaliduntil |
rolbypassrls | t
rolconfig |
oid | 10
-[ RECORD 2 ]--+-------------
rolname | sushi
rolsuper | f
rolinherit | t
rolcreaterole | f
rolcreatedb | f
rolcanlogin | t
rolreplication | f
rolconnlimit | -1
rolpassword | ********
rolvaliduntil |
rolbypassrls | f
rolconfig |
oid | 16384
-[ RECORD 3 ]--+-------------
rolname | drum
rolsuper | f
rolinherit | t
rolcreaterole | f
rolcreatedb | f
--More--
EDIT: I know that h takes me to the help. It says
b or ctrl-B Skip backwards k screenfuls of text [1]
but this does not work. Maybe because I'm in PuTTY?

You're probably using a $PAGER that doesn't support scrolling upwards. E.g. more.
Try executing postgresql client using a different PAGER variable:
PAGER=less psql [...]
Or:
export PAGER=less
psql [...]
If you want to make the change permanent, insert the above export line into your ~/.bash_profile.
Note: This will affect many things that use the $PAGER environment variable, but hey, it'll only enhance the experience right?

The --More-- indicator in the lower-left corner suggests that you're using the default pager more, which doesn't allow backward movement. You can switch to less from inside psql using this command:
\setenv PAGER 'less'
BTW, setting the pager to less -S (or typing -S and Enter inside less) will allow you to scroll sideways without wrapping (thus making the expanded mode unnecessary). And if you want to go fancy, you could use pspg :)

Related

How can we track progress of initial load of logical replication in postgreSQL

Is there a way to track the progress of inital load of logical replication in postgreSQL means the statistics about how much data data copied, how much data left to copied, any estimation time to complete or any error while copying ?
Thanks
Starting in the soon-to-be-released version, 14, you can use the view pg_stat_progress_copy to see the progress of the initial data copy for each table. It will report the data and tuples already copied, but will not give you that other info.
select * from pg_stat_progress_copy ;
pid | datid | datname | relid | command | type | bytes_processed | bytes_total | tuples_processed | tuples_excluded
-------+--------+---------+--------+-----------+----------+-----------------+-------------+------------------+-----------------
5,884 | 16,384 | jjanes | 17,014 | COPY FROM | CALLBACK | 1,240,918,397 | 0 | 12,628,295 | 0

Generating a markdown table with key bindings in Spacemacs

What is the best way to generate a markdown table with key bindings in Spacemacs (evil mode)?
Update: To clarify, this question is not about editing markdown, but automatically generating the table content for a large number of key bindings.
This could be an elisp function iterating through the possible single keystrokes (letters, numbers, punctuation, and possibly space and some control characters, with and without modifier keys), seeing what function each key is bound to (if any), and getting the description of the function.
You can do that manually using SPC h d k, but it would be handy to generate a table, given the number of possible key bindings and the way they can depend on the buffer mode and the state.
The table should show single keystrokes (letters, numbers, punctuation) with and without modifiers, the function bound to them, and the first line of the function description.
The result should look something like this:
https://github.com/cjolowicz/howto/blob/master/spacemacs.md
| Key | Mnemonic | Description | Function |
| ------ | -------- | --------------------------------------------------------------- | ------------------------ |
| a | *append* | Switch to Insert state just after point. | `evil-append` |
| b | *backward* | Move the cursor to the beginning of the COUNT-th previous word. | `evil-backward-word-begin` |
| c | *change* | Change text from BEG to END with TYPE. | `evil-change` |
| d | *delete* | Delete text from BEG to END with TYPE. | `evil-delete` |
| e | *end* | Move the cursor to the end of the COUNT-th next word. | `evil-forward-word-end` |
| f | *find* | Move to the next COUNT’th occurrence of CHAR. | `evil-find-char` |
| g | *goto* | (prefix) | |
| h | | Move cursor to the left by COUNT characters. | `evil-backward-char` |
| i | *insert* | Switch to Insert state just before point. | `evil-insert` |
| j | | Move the cursor COUNT lines down. | `evil-next-line` |
| k | | Move the cursor COUNT lines up. | `evil-previous-line` |
| l | | Move cursor to the right by COUNT characters. | `evil-forward-char` |
| m | *mark* | Set the marker denoted by CHAR to position POS. | `evil-set-marker` |
| n | *next* | Goes to the next occurrence. | `evil-ex-search-next` |
| o | *open* | Insert a new line below point and switch to Insert state. | `evil-open-below` |
| p | *paste* | Disable paste transient state if there is more than 1 cursor. | `evil-mc-paste-after` |
| q | | Record a keyboard macro into REGISTER. | `evil-record-macro` |
| r | *replace* | Replace text from BEG to END with CHAR. | `evil-replace` |
| s | *substitute* | Change a character. | `evil-substitute` |
| t | *to* | Move before the next COUNT’th occurrence of CHAR. | `evil-find-char-to` |
| u | *undo* | Undo changes. | `evil-tree-undo` |
| v | *visual* | Characterwise selection. | `evil-visual-char` |
| w | *word* | Move the cursor to the beginning of the COUNT-th next word. | `evil-forward-word-begin` |
| x | *cross* | Delete next character. | `evil-delete-char` |
| y | *yank* | Saves the characters in motion into the kill-ring. | `evil-yank` |
| z | *scroll* | (prefix) | |
(The Mnemonic column would of course be handcrafted.)
The orgtbl-mode minor mode that comes with Org (and therefore Emacs itself) should be able to help here. Activate it, then use Tab and Ret to navigate from cell to cell, letting orgtbl create and balance cells as you go. (Balancing happens when you navigate to a new cell, e.g. with Tab.)
You'll have to start the table yourself, e.g. with something like
| Key | Mnemonic | Description | Function |
|-
but from there orgtbl can take over. You can also use things like org-table-insert-column and org-table-move-row-down to make other kinds of tabular changes.
I'm not entirely sure how nicely this will play with evil-mode or what bindings it will use come out of the box, but it's worth a try.

watch in PostgreSQL with clear screen (cos)

\watch command is great in Postrges.
It appends an output of a query, being watched over and over.
For example, when I previously run a query SELECT id, nickname FROM users;, and entered \watch, in case of no new data, I get the same output over and over:
my_db=# SELECT id, nickname FROM users;
id | nickname
----+----------
1 | AntonAL
(1 row)
my_db=# \watch
четверг, 31 августа 2017 г. 13:23:26 (every 2s)
id | nickname
----+----------
1 | AntonAL
(1 row)
четверг, 31 августа 2017 г. 13:23:28 (every 2s)
id | nickname
----+----------
1 | AntonAL
(1 row)
It there any option to clear screen output, between watch executions?
I want to get dashboard-like experience, when a set of rows is displayed and refreshed, not appended over and over to the Postgres console output.
I had this problem, and with a suggestion from MatheusOl on #postgresql, the following works for me:
\C `clear`
SELECT * FROM pg_tables \watch 10
The \C [value] command sequence lets you set a title, and you can set that (shelling out with backticks) to the ANSI escapes returned by clear.
I don't think psql has any way to clear the terminal. Maybe do with bash?
while true;
clear;
psql -c "SELECT id, nickname FROM users"
sleep 2;
done
Similar to the last answer but a little simpler with the "watch" command (on linux) :
watch 'psql -c "SELECT id, nickname FROM users"'
... this will execute the query with psql every 2 seconds (default interval) and you can also add different parameters to this command :
$ watch --help
Usage:
watch [options] command
Options:
-b, --beep beep if command has a non-zero exit
-c, --color interpret ANSI color and style sequences
-d, --differences[=<permanent>]
highlight changes between updates
-e, --errexit exit if command has a non-zero exit
-g, --chgexit exit when output from command changes
-n, --interval <secs> seconds to wait between updates
-p, --precise attempt run command in precise intervals
-t, --no-title turn off header
-x, --exec pass command to exec instead of "sh -c"
-h, --help display this help and exit
-v, --version output version information and exit
For more details see watch(1).
I like to use this to monitor sessions activity with pg_stat_activity :
$ cat sessions.sql
select pid as process_id,
usename as username,
datname as database_name,
client_addr as client_address,
application_name,
backend_start,
state,
state_change
from pg_stat_activity;
$ watch -d psql -f sessions.sql
... and we get this output refreshed every 2 seconds :
Every 2.0s: psql -f sessions.sql Sun Sep 29 17:38:20 2019
process_id | username | database_name | client_address | application_name | backend_start | state | state_change
------------+----------+---------------+----------------+------------------+-------------------------------+--------+-------------------------------
1240 | postgres | | | | 2019-09-29 15:40:36.697364-04 | |
1236 | | | | | 2019-09-29 15:40:36.697681-04 | |
14025 | postgres | postgres | | psql | 2019-09-29 17:38:20.508996-04 | active | 2019-09-29 17:38:20.511069-04
1233 | | | | | 2019-09-29 15:40:36.69497-04 | |
1232 | | | | | 2019-09-29 15:40:36.695851-04 | |
1234 | | | | | 2019-09-29 15:40:36.690183-04 | |
(6 rows)

Can't rename table in RedShift

I run the following command through python3.3 psycopg2 package:
ALTER TABLE my_schema.tbl_old RENAME TO tbl_new
Before the command is run, my python code makes sure tbl_new does not exist.
However, I get the following error:
relation "tbl_new" already exists
Table structure:
bipilot=# select * from PG_TABLE_DEF where tablename='tbl_old';
schemaname | tablename | column | type | encoding | distkey | sortkey | notnull
------------+-----------+--------------+---------------+----------+---------+---------+---------
my_schema | tbl_old | price | numeric(10,2) | lzo | f | 0 | f
my_schema | tbl_old | price_date | date | none | f | 1 | f
my_schema | tbl_old | product_id | smallint | delta | f | 0 | f
No indices are defined.
Also, when I run the same RENAME command from psql shell, it works well.
Anyone can explain this strange behavior?
Thanks!!!
Problem found - bug in my python code - it was written 'tbl_Old' (instead of 'tbl_old').

PostgreSQL: Drop DB which exits with no parameters

I am trying from 3 days to drop the 'test123' DB which is not getting drop. Postgres reply of no such db exists.. i have no idea how it listed like this. and the db with \r
template1=# select * from pg_database;
datname | datdba | encoding | datcollate | datctype | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | dattablespace | datacl
-----------------------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+---------------+-------------------------------------
template1 | 10 | 6 | en_US.UTF-8 | en_US.UTF-8 | t | t | -1 | 12035 | 709 | 1663 | {=c/postgres,postgres=CTc/postgres}
template0 | 10 | 6 | en_US.UTF-8 | en_US.UTF-8 | t | f | -1 | 12035 | 709 | 1663 | {=c/postgres,postgres=CTc/postgres}
postgres | 10 | 6 | en_US.UTF-8 | en_US.UTF-8 | f | t | -1 | 12035 | 709 | 1663 |
PremierSuppliers | 16384 | 6 | en_US.UTF-8 | en_US.UTF-8 | f | t | -1 | 12035 | 709 | 1663 |
\r +| 16384 | 6 | en_US.UTF-8 | en_US.UTF-8 | f | t | -1 | 12035 | 709 | 1663 |
test123 | | | | | | | | | | |
Any workaround solution? to drop \r and test123 . i need to know what is the '+' placed in \r . and how to drop it. just to let you know that i already tried the steps given for similiar issue here but my problem not resolved.
The problematic database name seems to consist of a carriage return (ASCII code 13, shown as \r) followed by a newline (ASCII code 10) followed by the string test123
The sequence of characters 13,10 is the end of line in Windows.
The + sign at the end of the column is added by psql as a visual indication that the column continues on the next line. It's not part of the database name.
You may confirm the exact character codes by issuing:
SELECT encode(datname::bytea,'hex') from pg_database where datname like '%test123';
The expected result would be 0d0a74657374313233 if there are no spaces in addition to the CRLF. (otherwise please update the question with the actual result).
If using psql in Unix with readline capabilities, you may drop the database with this sequence of keystrokes:
DROP DATABASE "Ctrl+VCtrl+MEntertest123";Enter
When hitting Ctrl+VCtrl+M the screen should display ^M
EDIT:
If you can't manage it within psql in interactive mode, as an alternative this should also work from bash, to be launched by the postgres user or the database owner:
echo -e "DROP DATABASE \"\r\ntest123\";" | psql -d template1
The -e option to echo enables the interpretation of backslash escapes.