Before starting deployment, it is recommended to switch to the domestic image source according to Ubuntu, which can save a lot of precious time!

Install Postgresql

echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key Add-sudo apt-get update sudo apt-get install postgresql-9.6 # https://www.postgresql.org/download/linux/ubuntu/Copy the code

Modifying a Configuration File

Sudo vim/etc/postgresql / 9.6 / main/postgresql. Conf line like listen_addresses = '*' max_connections = 1000 logging_collector = on # # https://www.postgresql.org/docs/current/static/runtime-config.html more reference sudo vim/etc/postgresql / 9.6 / main/pg_hba. Conf Host all all 0.0.0.0/0 md5 # # https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html sudo more reference service postgresql restartCopy the code

Example Change the password of the default user Postgres

sudo -u postgres psql
# ALTER USER postgres WITH PASSWORD 'postgres';
# \q
exit
Copy the code

Set up the cluster

Setting up a cluster is not necessary, but if it is a production environment, it is recommended to do so, after all, data is more important than mount Tai!

The following is a simple one from the main one

The host ip
Masternode 10.10.10.10
Slavenode 10.10.10.9

After postgres is installed and configured on the Master and Slave nodes according to step 1, the cluster is set up.

The master node

1. Modify the configuration

Sudo vi/etc/postgresql / 9.6 / main/postgresql. Conf line like listen_addresses = '*' wal_level = hot_standby archive_mode = on archive_command = 'test ! -f/var/lib/postgresql f / 9.6 / archive / % && cp % p/var/lib/postgresql / 9.6 / archive / % f 'max_wal_senders = 16 Wal_keep_segments = 100 HOT_standby = on logging_collector = on ## https://www.postgresql.org/docs/current/static/runtime-config.html sudo vi/etc/postgresql / 9.6 / main/pg_hba. Conf host all All 10.0.0.0/8 md5 host replication repuser 10.0.0.0/8 MD5 ## https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html sudo -upostgres mkdir The/var/lib/postgresql / 9.6 / archive sudo chmod 0700 / var/lib/postgresql / 9.6 / archive sudo service postgresql restartCopy the code

2. Create a work accountrepuser

The slave node replicates through the repuser.

Sudo -upostgres createuser --replication repuser sudo -upostgres PSQL postgres=# \password repuser <password> ## https://www.postgresql.org/docs/current/static/user-manag.htmlCopy the code

Slave node

1. Stop the service

sudo service postgresql stop
Copy the code

2. Import data from the master node (Postgres can log in to the repuser role without a password)

Sudo upostgres vi/var/lib/postgresql /. Pgpass 10.10.10.10:5432: * : repuser: < password > 127.0.0.1:5432: * : repuser: < password > Sudo chmod 0600 / var/lib/postgresql /. Pgpass sudo mv/var/lib/postgresql / 9.6 / main/var/lib/postgresql / 9.6 / main bak sudo - upostgres pg_basebackup -d/var/lib/postgresql / / the main - F p - 9.6 X stream 10.10.10.10 - p - v - R - h 5432 - U repuserCopy the code

3. Modify the configuration

Sudo vi/var/lib/postgresql / 9.6 / main/recovery. The conf standby_mode = 'on' primary_conninfo = 'user = repuser host = 10.10.10.10 Port = 5432 'trigger_file =' failover. Now '# # https://www.postgresql.org/docs/current/static/recovery-config.html for more reference Sudo vi/etc/postgresql / 9.6 / main/postgresql. Conf hot_standby = onCopy the code

4. Restart and check the service

sudo service postgresql start sudo service postgresql status ... Active: Active (exited) sudo -upostgres PSQL PSQL (9.6.12)...Copy the code

Test cluster

Perform operations on the master node to check whether the slave node can replicate from the master node

Common commands

sudo service postgresql start
sudo service postgresql status
sudo service postgresql restart
Copy the code

More commonly used commands, you can check my another article: morning ops notes | Postgres commonly used commands

Docker and Kubernetes deploy PostgreSQL. Follow me and you’ll get the first push!

If this blog has been helpful to you, please remember to leave a comment + like + bookmark.

I am Chen, on the road of technology we forge ahead together!