Guest User

Untitled

a guest
Dec 14th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. # PostgreSQL Cheat Sheet
  2.  
  3. - [Common queries](#common-queries)
  4. - [Init DB](#init-db)
  5. - [Replication](#replication)
  6.  
  7.  
  8.  
  9. ### Common queries
  10.  
  11. ```bash
  12.  
  13. ```
  14.  
  15. ### Init DB
  16.  
  17. ```bash
  18.  
  19. su - postgres
  20. rm -rf /var/lib/postgresql/10/main/*
  21. /usr/lib/postgresql/10/bin/initdb -D /var/lib/postgresql/10/main/
  22. /usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main/ -l logfile start
  23.  
  24. ```
  25.  
  26. ### Replication
  27.  
  28. The primary server operates in continuous archiving mode,<br>
  29. while each standby server operates in continuous recovery mode, reading the WAL files from the primary.
  30.  
  31. - On both servers:
  32.  
  33. ```bash
  34. # In postgresql.conf:
  35. wal_level = replica
  36. hot_standby = on
  37. ```
  38.  
  39. - On the master:
  40.  
  41. ```bash
  42. # create repl user
  43. CREATE ROLE repl LOGIN REPLICATION ENCRYPTED PASSWORD 'verysecret';
  44.  
  45. # create a replication slot
  46. SELECT * FROM pg_create_physical_replication_slot('standby_replication_slot');
  47.  
  48. # Add to pg_hba.conf:
  49. host replication repl SLAVEIP/32 md5
  50. ```
  51.  
  52. - On the slave:
  53.  
  54. ```bash
  55. # In /var/lib/postgresql/10/main/recovery.conf
  56. primary_conninfo = 'host=MASTERIP port=5432 user=repl password=verysecret'
  57. primary_slot_name = 'standby_replication_slot'
  58. standby_mode = on
  59. trigger_file = '/etc/postgresql/10/main/failover.trigger'
  60.  
  61. # Init replica
  62. systemctl stop postgresql
  63. rm -rf /var/lib/postgresql/10/main/*
  64. su - postgres -c "pg_basebackup --wal-method=stream -D /var/lib/postgresql/10/main/ -U repl -h MASTERIP"
  65. ```
  66.  
  67. - Check replication status
  68.  
  69. - On the master:
  70.  
  71. Check the status of the replication:
  72.  
  73. ```bash
  74. select usename,application_name,client_addr,backend_start,state,sync_state from pg_stat_replication;
  75. ```
  76.  
  77. - On the slave:
  78.  
  79. pg_is_in_recovery will return "t" in the replica, and "f" in the master.
  80.  
  81. ```bash
  82. select pg_is_in_recovery();
  83. ```
  84.  
  85. The following query will return 1 in the slave, 0 in the master:
  86.  
  87. ```bash
  88. SELECT count(*) from pg_settings where name = 'hot_standby' AND setting = 'on';
  89. ```
Add Comment
Please, Sign In to add comment