Skip to content

[WIP] Major Upgrading of Supabase Postgres #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 95 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
b03ec3f
chore: remove dependency on ANXS postgres
dragarcia May 17, 2021
576be38
chore: Base compilation of Postgres 13 from source
dragarcia May 17, 2021
e505914
Merge branch 'develop' of github.com:supabase/postgres into feature/s…
dragarcia May 17, 2021
110ffda
chore: changing to Ubuntu 20.04 & r6g instance for ARM build
dragarcia May 19, 2021
3da399e
chore: add template files for Postgres
dragarcia May 19, 2021
48c7d7e
chore: update log_filename value in postgresql.conf
dragarcia May 19, 2021
afbc381
chore: update vars.yml
dragarcia May 19, 2021
f219047
chore: remove installation of AWS instance connect
dragarcia May 19, 2021
75ce04a
chore: completely remove dependency from anxs postgres
dragarcia May 19, 2021
8833ae1
chore: complete building of Postgres from source
dragarcia May 19, 2021
1971157
chore: temporarily disable other tasks
dragarcia May 19, 2021
caf81cd
chore: bump versions of existing extensions
dragarcia Jun 7, 2021
e02f704
chore: build PgBouncer from source instead
dragarcia Jun 7, 2021
2f62877
minor alterations to template postgresql.conf
dragarcia Jun 7, 2021
ef4bd1e
chore: Cleanup postgres setup
dragarcia Jun 7, 2021
62f9160
chore: uncomment other tasks
dragarcia Jun 7, 2021
c3c2564
chore: split setup-misc into its components
dragarcia Jun 7, 2021
03456f0
chore: regression - put back installation of EC2 instance connect
dragarcia Jun 8, 2021
476e711
chore: centralise checking of platform
dragarcia Jun 8, 2021
7dbb430
chore: adding postgrest
dragarcia Jun 8, 2021
b230080
chore: add symbolic links for PG binaries
dragarcia Jun 8, 2021
b8564e2
chore: update playbook.yml to reflect split of setup-misc
dragarcia Jun 8, 2021
d841291
chore: add timescaledb (Apache2 version) as an extension
dragarcia Jun 9, 2021
4eefed7
chore: add filters for pgbouncer & postgrest
dragarcia Jun 9, 2021
a5f9ac6
add var value for timescaledb
dragarcia Jun 9, 2021
dd57f4a
fix: /etc/postgresql should be owned by postgres
dragarcia Jun 9, 2021
6b24d6c
chore: add test script for postgres installation
dragarcia Jun 9, 2021
88651ea
fix: move installation of ec2-instance-connect to the end
dragarcia Jun 10, 2021
7529dbc
chore: clean up and consolidate configuration for PgBouncer
dragarcia Jun 10, 2021
53a129c
chore: boyscout separate versions of aws cli depending on architecture
dragarcia Jun 10, 2021
cc46c7d
fix: symbolic linking for Postgres binaries
dragarcia Jun 10, 2021
8a3513a
fix: adding dependencies for timescaledb
dragarcia Jun 10, 2021
3f1eaa5
chore: boyscout remove extra steps from postgrest
dragarcia Jun 10, 2021
c12c7b0
chore: #3 setupfail2ban filter against PG brute force logins
dragarcia Jun 14, 2021
3c09886
fix: ensure pgbouncer works upon systemctl start
dragarcia Jun 14, 2021
5fec0cf
fix: correct check for load and store exclusives
dragarcia Jun 14, 2021
9c999ba
chore: remove ansible role anxs/postgres
dragarcia Jun 15, 2021
62d2052
chore: ensure that postgres user has access to ssl certs
dragarcia Jun 16, 2021
3b10a44
chore: create extension internally instead
dragarcia Jun 16, 2021
c0d1573
fix: accidentally removed wal-g
dragarcia Jun 16, 2021
a8ad52b
fix: add in wal-g dependencies
dragarcia Jun 17, 2021
845f6b2
chore: #49 install supautils extension
dragarcia Jun 29, 2021
32adbf5
chore: #50 enable postgis_sfcgal extension
dragarcia Jun 29, 2021
dc51041
chore: #45 additional extension - PgRouting
dragarcia Jun 29, 2021
bf63a22
chore: cleanup
dragarcia Jun 30, 2021
7809843
Merge branch 'develop' of github.com:supabase/postgres into feature/s…
dragarcia Jun 30, 2021
114654e
fix: put stat_extensions.sql back in
dragarcia Jul 1, 2021
60c3660
chore: clean up extensions
dragarcia Jul 1, 2021
63055ca
fix: clean up duplicate variables
dragarcia Jul 1, 2021
cc13e64
chore: add pgbouncer fail2ban configuration
dragarcia Jul 1, 2021
372a7d3
disable login shell for postgres user
inian Jul 6, 2021
c3baeae
Clean up and consolidate code in supabase internal
dragarcia Jul 7, 2021
e15a205
disable timescaledb for the time being
dragarcia Jul 7, 2021
e373ef8
remove undefined variable 'platform_aws'
dragarcia Jul 7, 2021
de94ffb
remove timescaledb from postgresql.conf template
dragarcia Jul 7, 2021
5bad268
bump packer files to supabase postgres 13.3.0
dragarcia Jul 8, 2021
7b3ca76
fully purge away anxs.postgresql role
dragarcia Jul 8, 2021
7e08192
Clean up README - add new extensions and tidy up
dragarcia Jul 8, 2021
2670d7f
Make sure all PgBouncer related tasks are done under the tag 'install…
dragarcia Jul 8, 2021
bef9767
remove empty requirements.yml
dragarcia Jul 8, 2021
42d39e6
update ansible_arguments to omit building of postgrest
dragarcia Jul 8, 2021
2bf45c1
Update README
dragarcia Jul 8, 2021
3e72599
remove verbosity when building
dragarcia Jul 8, 2021
baa490f
update DO config for 13.3.0
dragarcia Jul 8, 2021
a350cbb
Merge branch 'develop' of github.com:supabase/postgres into feature/s…
dragarcia Jul 10, 2021
fcf8cf6
remove dependencies for pgrouting
dragarcia Jul 10, 2021
c05574a
set all install_recommends to no
dragarcia Jul 10, 2021
0c51785
chore: #56 build docker image with ansible
dragarcia Jul 10, 2021
4ee6241
chore: #56 retire previous way of building docker images
dragarcia Jul 10, 2021
6fdf095
chore: #58 settings to reduce memory overcommit
dragarcia Jul 12, 2021
13710e2
feat: add queries for additional metrics (#59)
darora Jul 15, 2021
1143d5a
Merge pull request #60 from supabase/da/bring-metrics-to-pg13
dragarcia Jul 15, 2021
05e02dd
postgresql-13-wal2json not available on ubuntu 20.04
dragarcia Jul 15, 2021
b61d72a
revert #58
dragarcia Jul 15, 2021
972a04d
consolidating pgbouncer changes
dragarcia Jul 15, 2021
c1ce89b
Merge branch 'feature/supabasePostgresV1.0' of github.com:supabase/po…
dragarcia Jul 15, 2021
f9ad755
feat: install jq as part of base image
darora Jul 15, 2021
0644163
feat: boot time optimizations
darora Jul 15, 2021
96d3466
chore: disable pgbouncer as well
darora Jul 15, 2021
1ba508f
Merge pull request #61 from supabase/da/boot-time-optimizations
dragarcia Jul 15, 2021
17ee8bc
Remove postgrest from internal optimization
dragarcia Jul 16, 2021
ad98355
run this task only under tag 'install-supabase-internal'
dragarcia Jul 16, 2021
d46af0e
rearrange tasks and set tags
dragarcia Jul 16, 2021
a0bf9ae
more consolidation of pgbouncer tasks
dragarcia Jul 16, 2021
979db2d
fix: update log rotation location and frequency (#62)
darora Jul 16, 2021
34cc677
fixes bug in docker
dragarcia Jul 19, 2021
c10fdcc
Merge branch 'feature/supabasePostgresV1.0' of github.com:supabase/po…
dragarcia Jul 19, 2021
5906050
fix: pg_stat_statements column names changed in PG13
darora Jul 19, 2021
a460893
Merge pull request #63 from supabase/da/fix-queries
dragarcia Jul 19, 2021
6aaeced
change postgres logging from stderr to csvlog
dragarcia Jul 26, 2021
1d8342b
update json config files
dragarcia Jul 26, 2021
17f55e4
remove github actions
dragarcia Jul 26, 2021
bfe92c5
update README.md
dragarcia Jul 26, 2021
cd4c9f4
remove additional --skip-tags,install-postgrest
dragarcia Jul 26, 2021
64db98f
Merge branch 'develop' of github.com:supabase/postgres into feature/s…
dragarcia Jul 26, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions .github/workflows/provision.yml

This file was deleted.

79 changes: 55 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,44 @@

Unmodified Postgres with some useful plugins. Our goal with this repo is not to modify Postgres, but to provide some of the most common extensions with a one-click install.

## Features

- ✅ Postgres [12](https://www.postgresql.org/about/news/1976/). Includes [generated columns](https://www.postgresql.org/docs/12/ddl-generated-columns.html) and [JSON path](https://www.postgresql.org/docs/12/functions-json.html#FUNCTIONS-SQLJSON-PATH) support.
- ✅ Ubuntu 18.04 (Bionic).
- ✅ [pg-contrib-12](https://www.postgresql.org/docs/12/contrib.html). Because everyone should enable `pg_stat_statements`.
## Primary Features
- ✅ Postgres [13](https://www.postgresql.org/about/news/postgresql-13-released-2077/).
- ✅ Ubuntu 20.04 (Focal Fossa).
- ✅ [wal_level](https://www.postgresql.org/docs/current/runtime-config-wal.html) = logical and [max_replication_slots](https://www.postgresql.org/docs/current/runtime-config-replication.html) = 5. Ready for replication.
- ✅ [PostGIS](https://postgis.net/). Postgres' most popular extension - support for geographic objects.
- ✅ [pgTAP](https://pgtap.org/). Unit Testing for Postgres.
- ✅ [pgAudit](https://www.pgaudit.org/). Generate highly compliant audit logs.
- ✅ [pgjwt](https://github.com/michelp/pgjwt). Generate JSON Web Tokens (JWT) in Postgres.
- ✅ [pgsql-http](https://github.com/pramsey/pgsql-http). HTTP client for Postgres.
- ✅ [plpgsql_check](https://github.com/okbob/plpgsql_check). Linter tool for PL/pgSQL.
- ✅ [plv8](https://github.com/plv8/plv8). Write in Javascript functions in Postgres.
- ✅ [PL/Java](https://github.com/tada/pljava). Write in Java functions in Postgres.
- ✅ [pg_cron](https://github.com/citusdata/pg_cron). Run CRON jobs inside Postgres.
- ✅ [pg-safeupdate](https://github.com/eradman/pg-safeupdate). Protect your data from accidental updates or deletes.
- ✅ [wal2json](https://github.com/eulerto/wal2json). JSON output plugin for logical replication decoding
- ✅ [Large Systems Extensions](https://github.com/aws/aws-graviton-getting-started#building-for-graviton-and-graviton2). Enabled for ARM images.

## Extensions
| Extension | Description |
| ------------- | ------------- |
| [Postgres contrib modules](https://www.postgresql.org/docs/current/contrib.html) | Because everyone should enable `pg_stat_statements`. |
| [PostGIS](https://postgis.net/) | Postgres' most popular extension - support for geographic objects. |
| [pgRouting](https://pgrouting.org/) | Extension of PostGIS - provides geospatial routing functionalities. |
| [pgTAP](https://pgtap.org/) | Unit Testing for Postgres. |
| [pg_cron](https://github.com/citusdata/pg_cron) | Run CRON jobs inside Postgres. |
| [pgAudit](https://www.pgaudit.org/) | Generate highly compliant audit logs. |
| [pgjwt](https://github.com/michelp/pgjwt) | Generate JSON Web Tokens (JWT) in Postgres. |
| [pgsql-http](https://github.com/pramsey/pgsql-http) | HTTP client for Postgres. |
| [plpgsql_check](https://github.com/okbob/plpgsql_check) | Linter tool for PL/pgSQL. |
| [pg-safeupdate](https://github.com/eradman/pg-safeupdate) | Protect your data from accidental updates or deletes. |
| [wal2json](https://github.com/eulerto/wal2json) | JSON output plugin for logical replication decoding. |
| [PL/Java](https://github.com/tada/pljava) | Write in Java functions in Postgres. |
| [plv8](https://github.com/plv8/plv8) | Write in Javascript functions in Postgres. |

Can't find your favorite extension? Suggest for it to be added into future versions [here](https://github.com/supabase/supabase/discussions/679)!

## Enhanced Security
Aside from having [ufw](https://help.ubuntu.com/community/UFW),[fail2ban](https://www.fail2ban.org/wiki/index.php/Main_Page), and [unattended-upgrades](https://wiki.debian.org/UnattendedUpgrades) installed, we also have the following enhancements in place:
| Enhancement | Description |
| ------------- | ------------- |
| fail2ban filter for PostgreSQL access | Monitors for brute force attempts over at port `5432`. |
| fail2ban filter for PgBouncer access | Monitors for brute force attempts over at port `6543`. |

## Additional Goodies
| Goodie | Description |
| ------------- | ------------- |
| [PgBouncer](https://postgis.net/) | Set up Connection Pooling. |
| [PostgREST](https://postgrest.org/en/stable/) | Instantly transform your database into an RESTful API. |
| [WAL-G](https://github.com/wal-g/wal-g#wal-g) | Tool for physical database backup and recovery. |

## Install

Expand All @@ -28,19 +49,29 @@ See all installation instructions in the [repo wiki](https://github.com/supabase
[![Digital Ocean](https://github.com/supabase/postgres/blob/master/docs/img/digital-ocean.png)](https://github.com/supabase/postgres/wiki/Digital-Ocean)
[![AWS](https://github.com/supabase/postgres/blob/master/docs/img/aws.png)](https://github.com/supabase/postgres/wiki/AWS-EC2)

### Building

Install the anxs.postgresql role
### Marketplace Images
| | Postgres & Extensions | PgBouncer | PostgREST | WAL-G |
|---|:---:|:---:|:---:|:---:|
| Supabase Postgres | ✔️ | ❌ | ❌ | ✔️ |
| Supabase Postgres: PgBouncer Bundle | ✔️ | ✔️ | ❌ | ✔️ |
| Supabase Postgres: PostgREST Bundle | ✔️ | ❌ | ✔️ | ✔️ |
| Supabase Postgres: Complete Bundle | ✔️ | ✔️ | ✔️ | ✔️ |

```bash
ansible-galaxy install anxs.postgresql -r ansible/install_roles.yml --force -vvv
```
#### Availability
| | AWS ARM | AWS x86 | Digital Ocean x86 |
|---|:---:|:---:|:---:|
| Supabase Postgres | Coming Soon | Coming Soon | Coming Soon |
| Supabase Postgres: PgBouncer Bundle | Coming Soon | Coming Soon | Coming Soon |
| Supabase Postgres: PostgREST Bundle | Coming Soon | Coming Soon | Coming Soon |
| Supabase Postgres: Complete Bundle | Coming Soon | Coming Soon | Coming Soon |

Set the `supabase_internal` flag to `false` to avoid baking in components that are specific to Supabase's hosted offering.
### Quick Build

```bash
$ time packer build -timestamp-ui \
-var "ansible_arguments=--skip-tags,update-only,-v,-e,supabase_internal='false'" \
--var "aws_access_key=<insert aws access key>" \
--var "aws_secret_key=<insert aws secret key>" \
--var "ami_regions=<insert desired regions>" \
amazon-arm.json
```

Expand Down
9 changes: 4 additions & 5 deletions amazon-arm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"aws_secret_key": "",
"region": "ap-northeast-1",
"ami_regions": "eu-central-1,eu-west-1,eu-west-2,ap-south-1,ap-southeast-1,ap-southeast-2,us-west-1,us-east-1,ca-central-1,sa-east-1,ap-northeast-1",
"ami": "ami-0d24b4f369844fc54",
"ami_name": "supabase-postgres-0.16.2",
"ami": "ami-076d8ebdd0e1ec091",
"ami_name": "supabase-postgres-13.3.0",
"environment": "prod",
"ansible_arguments": "--skip-tags,update-only -v"
"ansible_arguments": "--skip-tags,update-only,--skip-tags,install-postgrest,--skip-tags,install-pgbouncer,--skip-tags,install-supabase-internal"
},
"builders": [
{
Expand All @@ -17,7 +17,7 @@
"region": "{{user `region`}}",
"ami_regions": "{{user `ami_regions`}}",
"source_ami": "{{user `ami`}}",
"instance_type": "c6g.2xlarge",
"instance_type": "r6g.2xlarge",
"ssh_username": "ubuntu",
"ami_name": "{{user `ami_name`}}",
"tags": {
Expand Down Expand Up @@ -51,7 +51,6 @@
"execute_command": "echo 'packer' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'",
"type": "shell",
"scripts": [
"scripts/01-test",
"scripts/02-credentials_cleanup.sh",
"scripts/90-cleanup.sh",
"scripts/91-log_cleanup.sh"
Expand Down
6 changes: 3 additions & 3 deletions amazon.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"aws_secret_key": "",
"region": "af-south-1",
"ami_regions": "af-south-1",
"ami": "ami-07d30d5bf2d29a582",
"ami_name": "supabase-postgres-0.16.2",
"ami": "ami-08a4b40f2fe1e4b35",
"ami_name": "supabase-postgres-13.3.0.4",
"environment": "prod",
"ansible_arguments": "--skip-tags,update-only"
"ansible_arguments": "--skip-tags,update-only,--skip-tags,install-postgrest,--skip-tags,install-pgbouncer,--skip-tags,install-supabase-internal"
},
"builders": [
{
Expand Down
3 changes: 3 additions & 0 deletions ansible/files/docker_mnt/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cat /etc/postgresql/postgresql.conf > $PGDATA/postgresql.conf
echo "host replication $POSTGRES_USER 0.0.0.0/0 trust" >> $PGDATA/pg_hba.conf
echo "host all all 127.0.0.1/32 trust" >> $PGDATA/pg_hba.conf
2 changes: 2 additions & 0 deletions ansible/files/fail2ban_config/filter-pgbouncer.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Definition]
failregex = ^.+@<HOST>:.+error: password authentication failed$
3 changes: 3 additions & 0 deletions ansible/files/fail2ban_config/filter-postgresql.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Definition]
failregex = ^.*,.*,.*,.*,"<HOST>:.*password authentication failed for user.*$
ignoreregex = ^.*,.*,.*,.*,"127\.0\.0\.1.*password authentication failed for user.*$
7 changes: 7 additions & 0 deletions ansible/files/fail2ban_config/jail-pgbouncer.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[pgbouncer]
enabled = true
port = 6543
protocol = tcp
filter = pgbouncer
logpath = /var/log/pgbouncer.log
maxretry = 3
7 changes: 7 additions & 0 deletions ansible/files/fail2ban_config/jail-postgresql.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[postgresql]
enabled = true
port = 5432
protocol = tcp
filter = postgresql
logpath = /var/lib/postgresql/data/pg_log/postgresql.csv
maxretry = 3
7 changes: 3 additions & 4 deletions ansible/files/logrotate-postgres
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/var/lib/postgresql/12/main/pg_log/postgresql.csv {
daily
size 50M
/var/lib/postgresql/data/pg_log/postgresql.log {
size 50M
rotate 3
copytruncate
delaycompress
compress
notifempty
missingok
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
;;; Administrative settings
;;;

logfile = /var/log/postgresql/pgbouncer.log
pidfile = /var/run/postgresql/pgbouncer.pid
logfile = /var/log/pgbouncer.log
pidfile = /var/run/pgbouncer/pgbouncer.pid

;;;
;;; Where to wait for clients
Expand All @@ -56,10 +56,9 @@ listen_port = 6543

;; Unix socket is also used for -R.
;; On Debian it should be /var/run/postgresql
;unix_socket_dir = /tmp
unix_socket_dir = /tmp
;unix_socket_mode = 0777
;unix_socket_group =
unix_socket_dir = /var/run/postgresql

;;;
;;; TLS settings for accepting clients
Expand Down
40 changes: 40 additions & 0 deletions ansible/files/pgbouncer_config/pgbouncer.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Example systemd service unit for PgBouncer
#
# - Adjust the paths in ExecStart for your installation.
#
# - The User setting requires careful consideration. PgBouncer needs
# to be able to place a Unix-domain socket file where PostgreSQL
# clients will look for it. In the olden days, this was in /tmp,
# but systems using systemd now prefer something like
# /var/run/postgresql/. But then some systems also lock down that
# directory so that only the postgres user can write to it. That
# means you need to either
#
# - run PgBouncer as the postgres user, or
#
# - create a separate user and add it to the postgres group and
# make /var/run/postgresql/ group-writable, or
#
# - use systemd to create the sockets; see pgbouncer.socket nearby.
#
# For packagers and deployment systems, this requires some
# coordination between the PgBouncer and the PostgreSQL
# packages/components.
#
[Unit]
Description=connection pooler for PostgreSQL
Documentation=man:pgbouncer(1)
Documentation=https://www.pgbouncer.org/
After=network.target
#Requires=pgbouncer.socket

[Service]
Type=notify
User=postgres
ExecStart=/usr/local/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
#LimitNOFILE=1024

[Install]
WantedBy=multi-user.target
2 changes: 2 additions & 0 deletions ansible/files/pgbouncer_config/tmpfiles.d-pgbouncer.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Directory for PostgreSQL sockets, lockfiles and stats tempfiles
d /run/pgbouncer 2775 postgres postgres - -
Loading