tuning
[edozvola] / db.txt
1 qemu-img create -f qcow2 -o backing_file=db-backing.qcow2 -F qcow2 db-slave.qcow2
2
3
4
5 # network config
6
7 [dpavlin@localhost ~]$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
8 DEVICE=eth0
9 IPADDR=10.200.1.10
10 NETMASK=255.255.255.0
11 ONBOOT=yes
12 NAME=eth0
13
14 # install postgresql
15
16 yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
17
18 [root@localhost dpavlin]# yum install postgresql10-server
19
20
21 # initdb
22
23 [root@localhost data]# su postgres -c "/usr/pgsql-10/bin/initdb --lc-collate=hr_HR.utf8 -D /var/lib/pgsql/10/data/"
24 The files belonging to this database system will be owned by user "postgres".
25 This user must also own the server process.
26
27 The database cluster will be initialized with locales
28   COLLATE:  hr_HR.utf8
29   CTYPE:    en_US.UTF-8
30   MESSAGES: en_US.UTF-8
31   MONETARY: en_US.UTF-8
32   NUMERIC:  en_US.UTF-8
33   TIME:     en_US.UTF-8
34 The default database encoding has accordingly been set to "UTF8".
35 The default text search configuration will be set to "english".
36
37 Data page checksums are disabled.
38
39 fixing permissions on existing directory /var/lib/pgsql/10/data ... ok
40 creating subdirectories ... ok
41 selecting default max_connections ... 100
42 selecting default shared_buffers ... 128MB
43 selecting default timezone ... Europe/Zagreb
44 selecting dynamic shared memory implementation ... posix
45 creating configuration files ... ok
46 running bootstrap script ... ok
47 performing post-bootstrap initialization ... ok
48 syncing data to disk ... ok
49
50 WARNING: enabling "trust" authentication for local connections
51 You can change this by editing pg_hba.conf or using the option -A, or
52 --auth-local and --auth-host, the next time you run initdb.
53
54 Success. You can now start the database server using:
55
56     /usr/pgsql-10/bin/pg_ctl -D /var/lib/pgsql/10/data/ -l logfile start
57
58
59
60 # systemd
61
62 [root@localhost data]# systemctl status postgresql-10
63 ● postgresql-10.service - PostgreSQL 10 database server
64    Loaded: loaded (/usr/lib/systemd/system/postgresql-10.service; disabled; vendor preset: disabled)
65    Active: inactive (dead)
66      Docs: https://www.postgresql.org/docs/10/static/
67 [root@localhost data]# systemctl enable postgresql-10
68 Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-10.service to /usr/lib/systemd/system/postgresql-10.service.
69 [root@localhost data]# systemctl start postgresql-10
70 [
71
72 # streaming replication
73
74 https://www.cybertec-postgresql.com/en/setting-up-postgresql-streaming-replication/
75
76 sudo vi /var/lib/pgsql/10/data/postgresql.conf
77
78 ...
79
80 [root@slave data]# su postgres
81 bash-4.2$ pwd
82 /var/lib/pgsql/10/data
83 bash-4.2$ pg_basebackup -h 10.200.1.200 -U repuser -D /var/lib/pgsql/10/data/ --checkpoint=fast -R --slot=slave_slot -C
84 pg_basebackup: invalid option -- 'C'
85 Try "pg_basebackup --help" for more information.
86 bash-4.2$ pg_basebackup -h 10.200.1.200 -U repuser -D /var/lib/pgsql/10/data/ --checkpoint=fast -R --slot=slave_slot
87 pg_basebackup: could not send replication command "START_REPLICATION": ERROR:  replication slot "slave_slot" does not exist
88 pg_basebackup: child process exited with exit code 1
89 pg_basebackup: removing contents of data directory "/var/lib/pgsql/10/data/"
90
91
92
93 dpavlin=# select pg_create_physical_replication_slot('slave_slot') ;
94  pg_create_physical_replication_slot
95 -------------------------------------
96  (slave_slot,)
97 (1 row)
98
99
100 # wal archive for pitr
101
102 https://www.postgresql.org/docs/10/continuous-archiving.html
103
104 archive_command = 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'  # Unix
105
106
107 [root@slave 10]# mkdir -p /var/lib/pgsql/10/archive/
108 chown postgres:postgres /var/lib/pgsql/10/archive/
109
110 vi /var/lib/pgsql/10/data/postgresql.conf
111 i
112
113 # https://www.postgresql.org/docs/10/warm-standby.html#CONTINUOUS-ARCHIVING-IN-STANDBY
114
115 archive_mode = always               # enables archiving; off, on, or always
116 archive_command = 'test ! -f /var/lib/pgsql/10/archive/%f && cp %p /var/lib/pgsql/10/archive/%f'
117
118
119 [root@slave 10]# systemctl restart postgresql-10
120
121 dpavlin=# insert into test(t) select i from generate_series(100, 150) as i ;
122
123
124
125 dpavlin=# SELECT pg_start_backup('label', false, false);
126
127 SELECT * FROM pg_stop_backup(false, true);
128
129
130
131
132 [root@slave 10]# mkdir /var/lib/pgsql/10/archive.base/
133
134 # tuning
135
136 https://www.enterprisedb.com/blog/tuning-debian-ubuntu-postgresql
137
138 ## /etc/sysctl.conf
139
140 vm.swappiness = 10
141 vm.dirty_expire_centisecs = 500
142 vm.dirty_writeback_centisecs = 250
143 vm.dirty_ratio = 10
144 vm.dirty_background_ratio = 3
145 vm.overcommit_memory=2
146 net.ipv4.tcp_timestamps=0
147
148 /dev/mapper/pgdata-01-data /pgdata xfs  defaults,noatime,nodiratime 1 1
149
150 To activate it immediately, run:
151
152 mount -o remount,noatime,nodiratime /pgdata
153