{"id":21884005,"url":"https://github.com/mongoexpuser/ubuntu-postgresql-pgvector-image-and-containers","last_synced_at":"2026-04-13T06:02:20.983Z","repository":{"id":212231938,"uuid":"731002988","full_name":"MongoExpUser/Ubuntu-PostgreSQL-Pgvector-Image-and-Containers","owner":"MongoExpUser","description":"PostgreSQL Primary-Replica Servers (with Pgvector Extension) on Docker","archived":false,"fork":false,"pushed_at":"2024-10-03T07:56:55.000Z","size":2042,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-26T19:37:21.033Z","etag":null,"topics":["aws-lambda","bash","docker","docker-compose","nodejs","pgvector","postgresql","synchronous-replication","weather"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MongoExpUser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-13T06:27:58.000Z","updated_at":"2024-10-03T07:56:59.000Z","dependencies_parsed_at":"2024-01-18T22:43:02.244Z","dependency_job_id":"b4213cf2-4bf8-4b3b-979c-a8a312d86f37","html_url":"https://github.com/MongoExpUser/Ubuntu-PostgreSQL-Pgvector-Image-and-Containers","commit_stats":{"total_commits":33,"total_committers":1,"mean_commits":33.0,"dds":0.0,"last_synced_commit":"14997b9d1ff2d60c317a42d0e1c54cac85f03023"},"previous_names":["mongoexpuser/-ubuntu-postgressql-pgvector-image-and-containers","mongoexpuser/ubuntu-postgressql-pgvector-image-and-containers"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MongoExpUser%2FUbuntu-PostgreSQL-Pgvector-Image-and-Containers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MongoExpUser%2FUbuntu-PostgreSQL-Pgvector-Image-and-Containers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MongoExpUser%2FUbuntu-PostgreSQL-Pgvector-Image-and-Containers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MongoExpUser%2FUbuntu-PostgreSQL-Pgvector-Image-and-Containers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MongoExpUser","download_url":"https://codeload.github.com/MongoExpUser/Ubuntu-PostgreSQL-Pgvector-Image-and-Containers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244892352,"owners_count":20527432,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws-lambda","bash","docker","docker-compose","nodejs","pgvector","postgresql","synchronous-replication","weather"],"created_at":"2024-11-28T10:11:53.030Z","updated_at":"2025-10-16T03:01:45.174Z","avatar_url":"https://github.com/MongoExpUser.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Ubuntu-PostgreSQL-Pgvector-Image-and-Containers\n\n\u003cstrong\u003e Create Image and Deploy Containers for PostgreSQL 16 (1 Primary and 1 Replica) with pgvector extension and Manual Failover option for a Weather Application.\u003c/strong\u003e\n\n### Architectural Diagram Depicting Postgres Primary and Replica. The Replica is a Synchronous Standby - 'FIRST 1 (*)'\n![Image description](https://github.com/MongoExpUser/Ubuntu-PostgreSQL-Pgvector-Image-and-Containers/blob/main/pgsql-pgvector-lambda-arch.png)\n\n### 1) Build Image:                                                                                             \n     * Build\n       sudo docker build --no-cache -t weather/x86-64/ubuntu-22.04-postgres-16-pgvector:latest .\n\n### 2) Edit config files and TLS files in the following path in the repository, as deem necessary:    \n     - ./primary/cfg/pg_hba.conf         \n     - ./primary/tls/ca.pem:/etc/ssl/certs/ca.pem\n     - ./primary/tls/psql-cert.pem\n     - ./primary/tls/psql-cert.key\n     - ./replica/cfg/pg_hba.conf         \n     - ./replica/tls/ca.pem:/etc/ssl/certs/ca.pem\n     - ./replica/tls/psql-cert.pem\n     - ./replica/tls/psql-cert.key\n\n### 3) Deploy Containers and Destroy with Docker Compose:                                                                                             \n     * Deploy containers \n       set PWD=%cd% \u0026\u0026 sudo docker compose -f docker-compose-psql.yml --project-directory $PWD --project-name \"psql-weather-app\" up -d\n     \n     * Stop and remove containers with related network and volumes\n       set PWD=%cd% \u0026\u0026 sudo docker compose -f docker-compose-psql.yml --project-directory $PWD --project-name \"psql-weather-app\" down \u0026\u0026 sudo docker volume rm $(docker volume ls -q)\n\n### 4) Stop, Re-Start and Log Services with Docker Compose: \n     * Stop services\n       set PWD=%cd% \u0026\u0026 sudo docker compose -f docker-compose-psql.yml --project-directory $PWD --project-name \"psql-weather-app\" stop\n     \n     * Start services\n       set PWD=%cd% \u0026\u0026 sudo docker compose -f docker-compose-psql.yml --project-directory $PWD --project-name \"psql-weather-app\" start\n     \n     * Log: view output from containers\n       set PWD=%cd% \u0026\u0026 sudo docker compose -f docker-compose-psql.yml --project-directory $PWD --project-name \"psql-weather-app\" logs \n\n### 5) Check Replication Status After Deployment\n    * Primary and Replica PostgreSQL Nodes\n    sudo docker exec -it psql-node1 /bin/bash -c \"sudo -u postgres psql -c 'SELECT pid, state, client_addr, client_port, replay_lsn, sync_state FROM pg_stat_replication;'\"\n    sudo docker exec -it psql-node2 /bin/bash -c \"sudo -u postgres psql -c 'SELECT pid, status, receive_start_lsn, written_lsn, latest_end_lsn, latest_end_time, sender_host, sender_port  FROM pg_stat_wal_receiver;'\"\n\n\n### 6) Confirmation\n    * Primary and Replica PostgreSQL Nodes\n      Primary State: If the value of \"state\" is  \"streaming\" and the values of the remaining parameters are displayed, then the primary is okay and replicating.\n      Replica Status: If the value of \"status\" is \"streaming\" and the values of the remaining parameters are displayed, it implies the replica is also okay and it is receiving replication data.\n\n\n### 7) Inspect the Postgres Services and the Container Logs:\n     * Primary and Replica PostgreSQL Nodes\n       sudo docker exec -it psql-node1 /bin/bash -c  \"sudo tail -n 600 -f  /var/log/postgresql/postgresql-16-main.log\"\n       sudo docker exec -it psql-node2 /bin/bash -c  \"sudo tail -n 600 -f  /var/log/postgresql/postgresql-16-main.log\"\n       sudo docker logs psql-node1 \n       sudo docker logs psql-node2\n\n### 8) Interact with Containers/Connect to Containers:                                                                                             \n     * Primary and Replica PostgreSQL Nodes\n       sudo docker exec -it psql-node1 /bin/bash\n       sudo docker exec -it psql-node2 /bin/bash\n     \n### 9) Connect to PostgreSQL Servers From the Host:                                                                                          \n     * Primary and Replica PostgreSQL Nodes\n       sudo docker exec -it psql-node1 /bin/bash -c \"sudo -u postgres psql\"\n       sudo docker exec -it psql-node2 /bin/bash -c \"sudo -u postgres psql\"\n\n### 10) Check Configuration Information: Run Directly with Bash:                                                                                                                    \n    * Postgresql.conf on  the Primary and Replica PostgreSQL Nodes\n      sudo docker exec -it psql-node1 /bin/bash -c \"sudo -u postgres psql -c 'TABLE pg_file_settings;'\"\n      sudo docker exec -it psql-node2 /bin/bash -c \"sudo -u postgres psql -c 'TABLE pg_file_settings;'\"\n    * Pg_hba.conf on the Primary and Replica PostgreSQL Nodes\n      sudo docker exec -it psql-node1 /bin/bash -c \"sudo -u postgres psql -c 'TABLE pg_hba_file_rules;'\"\n      sudo docker exec -it psql-node2 /bin/bash -c \"sudo -u postgres psql -c 'TABLE pg_hba_file_rules;'\"\n\n### 11) Test Failover:\n    * Run the following commands on the Standby/Replica (node 2) and promote into Primary.\n     sudo docker exec -it psql-node2 /bin/bash -c 'sudo -u postgres psql -c \"SELECT pg_promote(wait := FALSE);\"'\n     sudo docker exec -it psql-node2 /bin/bash -c \"sudo -u postgres psql -c \\\"ALTER SYSTEM SET synchronous_commit TO off;\\\" \"\n     sudo docker exec -it psql-node2 /bin/bash -c \"sudo -u postgres psql -c \\\"SELECT * FROM pg_create_physical_replication_slot('main_slot');\\\" \"\n     sudo docker exec -it psql-node2 /bin/bash -c 'sudo service postgresql restart'\n     sudo docker exec -it psql-node2 /bin/bash -c \"sudo -u postgres psql -c \\\"ALTER SYSTEM SET synchronous_commit TO on;\\\" \"\n    * The above 5 statements can also be put into a bash script (promote-standy.sh) and executed on the docker host directly. See the repository.\n    * The application(s) can then be switched to point to the NEW Primary (i.e OLD Replica/Standby).\n\n ### 12) Production Failover:    \n    * In production deployment, the database connection logic can be written to ensure that the primary is active before any write or ready operation. \n      E.g Within try-catch-finally statement and in combination with either switch or if-else statement.\n    * If not active (that is, down), a promotion of the replica/standy to primary can be initiated  with the bash script above (promote-standy.sh). See the repository.\n    * After that, application(s) connection string can then be pointed to the NEW Primary (i.e OLD Replica/Standby).\n\n ### 13) Production Rebuild of Standy/Replica:   \n    * After failover, a new Standy/Replica can be rebuilt, on the old primary node or on a another node, with either:\n      1) pg_basebackup: https://www.postgresql.org/docs/current/app-pgbasebackup.html or\n      2) pg_rewind: https://www.postgresql.org/docs/current/app-pgrewind.html\n    * The steps for rebuilding the new Standy/Replica can also be scripted as a bash script.\n      \n    \n\n\n# License\n\nCopyright © 2023. MongoExpUser\n\nLicensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoexpuser%2Fubuntu-postgresql-pgvector-image-and-containers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmongoexpuser%2Fubuntu-postgresql-pgvector-image-and-containers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoexpuser%2Fubuntu-postgresql-pgvector-image-and-containers/lists"}