watch in PostgreSQL with clear screen (cos) - postgresql

\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)

Related

Postgres query is getting lock while executing through shell script

The below query is getting locked by executing through perl
ALTER TABLE base_table
DETACH PARTITION part_table;
By using this query
select pid,state,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;
pid | state | usename | blocked_by | blocked_query
-------+--------+----------+------------+------------------------------------
20521 | active | parttest | {20306} | ALTER TABLE BASE +
| | | | DETACH PARTITION part_table
| | | |
(1 row)
Can you please suggest me how to solve this issue?

Unable to create new database

When I create a new mysql db, slashdb's test connection fails.
Here is how I log into mysql:
$ mysql -u 7stud -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.5.5-10.4.13-MariaDB Homebrew
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| chat |
| ectoing_repo |
| ejabberd |
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
8 rows in set (0.00 sec)
mysql> use mydb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| cheetos |
| greetings |
| mody |
| people |
+----------------+
4 rows in set (0.00 sec)
mysql> select * from people;
+----+--------+------+
| id | name | info |
+----+--------+------+
| 1 | 7stud | abc |
| 2 | Beth | xxx |
| 3 | Diane | xyz |
| 4 | Kathy | xyz |
| 5 | Kathy | xyz |
| 6 | Dave | efg |
| 7 | Tom | zzz |
| 8 | David | abc |
| 9 | Eloise | abc |
| 10 | Jess | xyz |
| 11 | Jeffsy | 2.0 |
| 12 | XXX | xxx |
| 13 | XXX | xxx |
+----+--------+------+
13 rows in set (0.00 sec)
In the slashdb form for creating a new database, here is the info I entered:
Hostname: 127.0.0.1
Port: 80
Database Login: 7stud
Database Password: **
Database Name: mydb
Then I hit the "Test Connection" button, whereupon I get a spinning wheel, which disappears after a few minutes, but no "Connection Successful" message. What am I doing wrong?
Now, I'm using port 3306:
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
but when slashdb tries to connect, I get the error:
Host localhost:3306 is not accessible
Your port is wrong in the database connection. You said that your MySQL is configured on port 3306, but you also posted your SlashDB config for database with port 80. Please change that to 3306.
Also, not sure how if you don't need to enable remote access to MySQL. Even if your SlashDB is running on the same machine as the MySQL database, it uses TCP/IP to connect.

Datadog: PostgreSQL custom_metrics returns a single row

I wanted to create a graph in Datadog to display iddle connections per user.
Following this example: http://www.miketheman.net/tag/postgres/ I changed my postgres.yaml configuration to:
init_config:
instances:
- host: 127.0.0.1
port: 5432
username: datadog
password: '**************'
tags:
- environment:qa
- role:db
custom_metrics:
- # Postgres Connection state
descriptors:
- [datname, database]
- [usename, user]
- [state, state]
metrics:
COUNT(state): [postgresql.connection_state, GAUGE]
query: >
SELECT datname, usename, state, %s FROM pg_stat_activity
GROUP BY datname, usename, state HAVING COUNT(state) > 0;
relation: false
I can see the metric is appearing in Datadog, but I can just see one of the rows that should appear (has I have several databases in my PostgreSQL).
Here is the datadog connection graph
Am I missing any step? Is postgres.yaml missing any configuration?
Running that query in psql, I get this (modifying names but not data):
postgres=# SELECT datname, usename, state, COUNT(state) FROM pg_stat_activity GROUP BY datname, usename, state HAVING COUNT(state) > 0;
datname | usename | state | count
---------------------+-------------+---------------------+-------
compan_strawberr_qa | compan | idle in transaction | 1
compan_qa | compan | idle | 130
pineappplee_qa | compan | idle | 10
compan_strawberr_qa | compan | idle | 29
compan_qa | watermel | active | 1
pineappplee_qa | pineappplee | idle | 10
pear_qa | pear | idle | 6
postgres | postgres | active | 1
postgres | datadog | idle | 1
apple_qa | apple_qa | idle | 1
bblluebberrriiess | compan | idle | 3
compan_ser_qa | ser_qa | idle | 4
compan_service_qa | compan | idle | 26
(13 rows)
I found the solution: the user datadog didnt have permission to read connections that wasnt form him. So it was just getting a single row.
I gave permissions for that user to read pg_stat_activity

PostgreSQL: How to scroll up in long output

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 :)

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.