May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
How to connect to postgres db:
|
||||
|
||||
set PATH if not done:
|
||||
|
||||
postgres$ export PATH=/Library/PostgreSQL/10/bin:$PATH
|
||||
postgres$ which psql
|
||||
/Library/PostgreSQL/10/bin/psql
|
||||
|
||||
connect to db using SYNTAX - psql -d -U
|
||||
|
||||
postgres$ psql -d edb -U postgres
|
||||
Password for user postgres:
|
||||
psql (10.13)
|
||||
Type "help" for help.
|
||||
|
||||
postgres=#
|
||||
|
||||
Find current connection info:
|
||||
|
||||
postgres=# \conninfo
|
||||
You are connected to database "edb" as user "postgres" via socket in "/tmp" at port "5432".
|
||||
|
||||
|
||||
|
||||
postgres=# select current_schema,current_user,session_user,current_database();
|
||||
current_schema | current_user | session_user | current_database
|
||||
----------------+--------------+--------------+------------------
|
||||
public | postgres. | postgres | edb
|
||||
|
||||
|
||||
|
||||
Switch to another database:
|
||||
|
||||
postgres-# \c dbaclass
|
||||
You are now connected to database "dbaclass" as user "postgres".
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
DATA_DIRECTORY - > Specifies the directory to use for data storage.
|
||||
|
||||
dbaclass=# show data_directory;
|
||||
|
||||
data_directory
|
||||
-----------------------------
|
||||
/Library/PostgreSQL/10/data
|
||||
(1 row)
|
||||
|
||||
dbaclass=# select setting from pg_settings where name = 'data_directory';
|
||||
setting
|
||||
-----------------------------
|
||||
/Library/PostgreSQL/10/data
|
||||
(1 row)
|
||||
|
||||
-- This will show location of important files in postgres
|
||||
|
||||
dbaclass=# SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
|
||||
@@ -0,0 +1,21 @@
|
||||
- Drop database from psql
|
||||
Note - while dropping a database, you need to connect to a database other than the db you are trying to drop.
|
||||
|
||||
postgres=# \conninfo
|
||||
You are connected to database "postgres" as user "postgres" on host "localhost" at port "5432".
|
||||
|
||||
postgres#drop database "DBACLASS";
|
||||
|
||||
-- Drop database using dropdb os utility
|
||||
|
||||
postgres$ pwd
|
||||
/Library/PostgreSQL/10/bin
|
||||
|
||||
postgres$ ./dropdb -e "DBACLASS"
|
||||
Password:
|
||||
SELECT pg_catalog.set_config('search_path', '', false)
|
||||
DROP DATABASE "DBACLASS";
|
||||
|
||||
|
||||
|
||||
For complete article visit - > https://dbaclass.com/article/drop-database-postgres/
|
||||
@@ -0,0 +1,19 @@
|
||||
1. Create directory for archiving:
|
||||
|
||||
mkdir -p /Library/PostgreSQL/10/data/archive/
|
||||
|
||||
2. Update the postgres.conf file with below values
|
||||
|
||||
wal_level = replica
|
||||
archive_mode = on
|
||||
max_wal_senders=1
|
||||
archive_command= 'test ! -f /Library/PostgreSQL/10/data/archive/%f && cp %p /Library/PostgreSQL/10/data/archive/%f'
|
||||
|
||||
3. Restart the postgres servers
|
||||
|
||||
export PGDATA=/Library/PostgreSQL/10/data
|
||||
pg_ctl stop
|
||||
pg_ctl start
|
||||
|
||||
3. Check archive status:
|
||||
postgres=# select name,setting from pg_settings where name like 'archive%';
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Find list of installed extension:
|
||||
|
||||
psql# \dx
|
||||
|
||||
(or)
|
||||
|
||||
psql#\dx+
|
||||
|
||||
(or)
|
||||
|
||||
psql#SELECT * FROM pg_extension;
|
||||
|
||||
-- For finding available extension in server:
|
||||
|
||||
psql# SELECT * FROM pg_available_extensions;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- QUERY TO FIND BLOCKING SESSION DETAILS
|
||||
|
||||
dbaclass#select pid as blocked_pid, usename, pg_blocking_pids(pid) as "blocked_by(pid)", query as blocked_query from pg_stat_activity where cardinality(pg_blocking_pids(pid)) > 0;
|
||||
@@ -0,0 +1,32 @@
|
||||
Find location of postgres related conf files
|
||||
|
||||
dbaclass=# SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
|
||||
name | setting
|
||||
-------------------+---------------------------------------------
|
||||
config_file | /Library/PostgreSQL/10/data/postgresql.conf
|
||||
data_directory | /Library/PostgreSQL/10/data
|
||||
external_pid_file |
|
||||
hba_file | /Library/PostgreSQL/10/data/pg_hba.conf
|
||||
ident_file | /Library/PostgreSQL/10/data/pg_ident.conf
|
||||
(5 rows)
|
||||
|
||||
alternatively:
|
||||
|
||||
postgres=# show config_file;
|
||||
|
||||
config_file
|
||||
--------------------------------------------
|
||||
|
||||
/Library/PostgreSQL/10/data/postgresql.conf
|
||||
|
||||
(1 row)
|
||||
|
||||
postgres=# show hba_file;
|
||||
|
||||
hba_file
|
||||
-----------------------------------------
|
||||
/Library/PostgreSQL/10/data/pg_hba.conf
|
||||
|
||||
(1 row)
|
||||
|
||||
postgres=# show ident_file;
|
||||
@@ -0,0 +1 @@
|
||||
select t.relname,l.locktype,page,virtualtransaction,pid,mode,granted from pg_locks l, pg_stat_all_tables t where l.relation=t.relid order by relation asc;
|
||||
@@ -0,0 +1,25 @@
|
||||
Find sessions in the postgres:
|
||||
|
||||
select pid as process_id,
|
||||
usename as username,
|
||||
datname as database_name,
|
||||
client_addr as client_address,
|
||||
application_name,
|
||||
backend_start,
|
||||
state,
|
||||
state_change,query
|
||||
from pg_stat_activity;
|
||||
|
||||
|
||||
|
||||
-- For specific database:
|
||||
|
||||
select pid as process_id,
|
||||
usename as username,
|
||||
datname as database_name,
|
||||
client_addr as client_address,
|
||||
application_name,
|
||||
backend_start,
|
||||
state,
|
||||
state_change,query
|
||||
from pg_stat_activity where datname='dbaclass';
|
||||
@@ -0,0 +1,5 @@
|
||||
postgres=# SELECT pg_database.datname as "database_name", pg_size_pretty(pg_database_size(pg_database.datname)) AS size_in_mb FROM pg_database ORDER by size_in_mb DESC;
|
||||
|
||||
(or)
|
||||
|
||||
postgres=# \l+
|
||||
@@ -0,0 +1,7 @@
|
||||
-- First find the pid of the session:
|
||||
|
||||
dbaclass#SELECT datname as database, pid as pid, usename as username, application_name , client_addr , query FROM pg_stat_activity;
|
||||
|
||||
<< Lets say the pid=1124, in the below query pass the pid value to kill that particular session..>>
|
||||
|
||||
dbaclass#select pg_terminate_backend(pid) from pg_stat_activity where pid='1123';
|
||||
@@ -0,0 +1,8 @@
|
||||
-- Here we want to kill all session of the user postgres
|
||||
|
||||
-- List all the session of that user.
|
||||
dbaclass#select datname as database, pid as pid, usename as username, application_name , client_addr, query FROM pg_stat_activity where username='postgres';
|
||||
|
||||
-- Kill all the session of user postgres.
|
||||
|
||||
dbaclass#select pg_terminate_backend(pid) from pg_stat_activity where usename='postgres';
|
||||
@@ -0,0 +1,17 @@
|
||||
-- Last pg config reload time
|
||||
|
||||
postgres=# select pg_conf_load_time() ;
|
||||
pg_conf_load_time
|
||||
----------------------------------
|
||||
2020-07-06 13:20:18.048689+05:30
|
||||
(1 row)
|
||||
|
||||
-- Reload again and see whether reload time changed or not
|
||||
|
||||
postgres=# select pg_reload_conf();
|
||||
pg_reload_conf
|
||||
----------------
|
||||
t
|
||||
(1 row)
|
||||
|
||||
postgres=# select pg_conf_load_time() ;
|
||||
@@ -0,0 +1,13 @@
|
||||
postgres=# \list+
|
||||
List of databases
|
||||
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
|
||||
-----------+----------+----------+---------+-------+-----------------------+---------+------------+--------------------------------------------
|
||||
dbaclass | postgres | UTF8 | C | C | | 2268 MB | pg_default |
|
||||
postgres | postgres | UTF8 | C | C | | 4132 MB | pg_default | default administrative connection database
|
||||
template0 | postgres | UTF8 | C | C | =c/postgres +| 7601 kB | pg_default | unmodifiable empty database
|
||||
| | | | | postgres=CTc/postgres | | |
|
||||
template1 | postgres | UTF8 | C | C | =c/postgres +| 7601 kB | pg_default | default template for new databases
|
||||
| | | | | postgres=CTc/postgres | | |
|
||||
(4 rows)
|
||||
|
||||
postgres=# select datname from pg_database;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Below command signals the log-file manager to switch to a new output file immediately.it is just like an alert log
|
||||
|
||||
select pg_rotate_logfile() ;
|
||||
@@ -0,0 +1,21 @@
|
||||
dbaclass=# show server_version;
|
||||
server_version
|
||||
----------------
|
||||
10.13
|
||||
(1 row)
|
||||
|
||||
dbaclass=# select version ();
|
||||
version
|
||||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
PostgreSQL 10.13 on x86_64-apple-darwin, compiled by i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00), 64-bit
|
||||
(1 row)
|
||||
|
||||
postgres$ cd /Library/PostgreSQL/10/bin
|
||||
|
||||
postgres$ ./postgres -V
|
||||
postgres (PostgreSQL) 10.13
|
||||
|
||||
-- PG_VERSION file is under data directory
|
||||
postgres$ cd /Library/PostgreSQL/10/data
|
||||
|
||||
postgres$ cat PG_VERSION
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Monitor query execution time
|
||||
|
||||
select substr(query,1,100) query,calls,min_time/1000 "min_time(in sec)" , max_time/1000 "max_time(in sec)", mean_time/1000 "avg_time(in sec)", rows from pg_stat_statements order by mean_time desc;
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
NOTE:
|
||||
|
||||
If pg_stat_statements is not available in your database, then activate using below:
|
||||
-- Add below parameters in postgres.conf file and restart the postgres cluster
|
||||
|
||||
shared_preload_libraries = 'pg_stat_statements'
|
||||
pg_stat_statements.track = all
|
||||
|
||||
sudo service postgresql restart
|
||||
|
||||
-- Now create extension:
|
||||
|
||||
dbaclass=# create extension pg_stat_statements;
|
||||
@@ -0,0 +1,11 @@
|
||||
-- Uptime of server
|
||||
|
||||
postgres# SELECT now() - pg_postmaster_start_time() "uptime";
|
||||
uptime
|
||||
------------------------
|
||||
9 days 04:54:56.774981
|
||||
(1 row)
|
||||
|
||||
-- Server startup time:
|
||||
|
||||
postgres# SELECT pg_postmaster_start_time();
|
||||
@@ -0,0 +1 @@
|
||||
select pg_switch_wal();
|
||||
@@ -0,0 +1,34 @@
|
||||
-- Below commands can be used to find postgres db date/timestamp
|
||||
|
||||
postgres=# SELECT CURRENT_TIMESTAMP;
|
||||
current_timestamp
|
||||
----------------------------------
|
||||
2020-07-06 17:45:24.929293+05:30
|
||||
(1 row)
|
||||
|
||||
postgres=# select current_date;
|
||||
current_date
|
||||
--------------
|
||||
2020-07-06
|
||||
(1 row)
|
||||
|
||||
postgres=# select statement_timestamp() ;
|
||||
statement_timestamp
|
||||
----------------------------------
|
||||
2020-07-06 17:46:16.825492+05:30
|
||||
(1 row)
|
||||
|
||||
postgres=# select timeofday() ;
|
||||
timeofday
|
||||
-------------------------------------
|
||||
Mon Jul 06 17:46:23.861551 2020 IST
|
||||
(1 row)
|
||||
|
||||
postgres=# select localtime(0);
|
||||
localtime
|
||||
-----------
|
||||
17:52:38
|
||||
(1 row)
|
||||
|
||||
postgres=# select localtimestamp(0);
|
||||
localtimestamp
|
||||
@@ -0,0 +1,12 @@
|
||||
dbaclass=# show timezone
|
||||
TimeZone
|
||||
--------------
|
||||
Asia/Kolkata
|
||||
(1 row)
|
||||
|
||||
dbaclass=# SELECT current_setting('TIMEZONE');
|
||||
current_setting
|
||||
-----------------
|
||||
Asia/Kolkata
|
||||
|
||||
dbaclass=# select name,setting,short_desc,boot_val from pg_settings where name='TimeZone';
|
||||
Reference in New Issue
Block a user