PostgreSQL Database Setup

Installs PostgreSQL database with optimized configuration

Script Author

Rowan de Haas's avatar
Rowan de Haas
Script Author

Script Details

Created 8 months ago
Size 1 KB

Tags

Script Content

Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#cloud-config
package_update: true
package_upgrade: true

packages:
  - postgresql
  - postgresql-contrib
  - postgresql-client

write_files:
  - path: /etc/postgresql/14/main/postgresql.conf
    content: |
      listen_addresses = 'localhost'
      port = 5432
      max_connections = 100
      shared_buffers = 128MB
      effective_cache_size = 4GB
      maintenance_work_mem = 64MB
      checkpoint_completion_target = 0.9
      wal_buffers = 16MB
      default_statistics_target = 100
      random_page_cost = 1.1
      effective_io_concurrency = 200
    append: false

runcmd:
  - systemctl enable postgresql
  - systemctl start postgresql
  # Create application database and user
  - sudo -u postgres createdb appdb
  - sudo -u postgres psql -c "CREATE USER appuser WITH PASSWORD 'SecurePassword123!';"
  - sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE appdb TO appuser;"
  - systemctl restart postgresql

How to Use This Script

Cloud Provider Examples

Amazon EC2

aws ec2 run-instances
  --image-id ami-12345678
  --instance-type t3.micro
  --user-data file://script.yaml

DigitalOcean

doctl compute droplet create
  --image ubuntu-22-04-x64
  --size s-1vcpu-1gb
  --user-data-file script.yaml
  my-droplet

Google Cloud

gcloud compute instances create
  my-instance
  --metadata-from-file
  user-data=script.yaml