PostgreSQL - setup, admin and developer notes

Overview

This is a notes that I have been keeping since 2023. Hope you find it useful.

Installation

Installation on Ubuntu

Installation on Windows

Running a server instance

Notes: you may download the latest installer (from the link below) and install it. In that case, you may skip the following manual steps.

https://www.postgresql.org/download/windows/

If you want to try to set it up manually so that you can understand more about the Postgres runtime, please follow the steps below:

Using cli

Security settings

Server admin

Backup and restore

Server configuration

Extensions

Accessing another database

Logical replication

Date: 23.Sep.2024 Information:

  1. Edit postgres.conf in the primary server:

    listen_addresses = '*'
    wal_level = logical
    max_wal_senders = 10
    

    For security wise, use this setting:

    listen_addresses = 'localhost, db_master_private_ip_address'
    
  2. Edit the pg_hba.conf:

    # TYPE  DATABASE        USER            ADDRESS                 METHOD
    # IPv4 local connections:
    host    all             all             127.0.0.1/32            trust
    host    all             all             0.0.0.1/0            	scram-sha-256
    

    For security wise, use this setting, replace '0.0.0.1/0' with the remote IP address.

    host    all             all             db_master_private_ip_address/32            	scram-sha-256
    host    all             all             192.168.68.54/32                            scram-sha-256
    
  3. In the firewall, open port 5432.

    In Linux

    sudo ufw allow from db_replica_private_ip_address to any port 5432
    

    In Windows, open Windows Defender Firewall with Advanced Security to open the port.

  4. In the primary server, create a role specifically for replication process:

    CREATE ROLE my_user_id WITH REPLICATION LOGIN PASSWORD 'ciysys123';
    GRANT ALL PRIVILEGES ON DATABASE my_db1 TO my_user_id;
    GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO my_user_id;
    

    Grant CREATE TABLE permission:

    GRANT ALL ON SCHEMA public TO my_user_id;
    
  5. Restart primary server.

  6. Now, we are going to publish the 'sales' table which has the following structure:

    create table sales (prod_id int not null, qty int, unit_price numeric(15,2));
    

    NOTES: this table does not have primary key yet! Will create it later.

    Do this in primary server:

    CREATE PUBLICATION my_publication;
    ALTER PUBLICATION my_publication ADD TABLE sales;
    
  7. In the secondary server,

    CREATE SUBSCRIPTION my_subscription
    CONNECTION 'host=192.168.68.55 port=5432 password=ciysys123 user=my_user_id dbname=test_repl application_name=mysub1'
    PUBLICATION my_publication;
    
  8. To test it out, run the following command in the primary server,

    insert into sales (prod_id, qty, unit_price) values
        (1, 10, 1.2)
        , (2, 10, 1.2)
        , (3, 10, 1.2)
        , (4, 10, 1.2);
    
  9. In the secondary server,

    select * from sales;
    
  10. Monitoring the replication.

    • In the primary server,

      select * from pg_publication
      select * from pg_publication_tables
      
      --this returns the current LSN value which appears in pg_stat_replication
      -- & also pg_stat_subscription (in the secondary server).
      select * from pg_current_wal_lsn()
      

      For monitoring who has subscribed and the replication state:

      select * from pg_stat_replication
      
    • In the secondary server,

      What has been subscribed:

      select * from pg_subscription
      

      For monitoring:

      select * from pg_stat_subscription
      

FAQ for replication

FAQ

References

Back to #POSTGRES blog

Back to #blog listing