|
| 1 | +# PostgreSQL Setup |
| 2 | + |
| 3 | +The purpose of this guide is to show how _we_ setup Postgres on a standard VPS. |
| 4 | +No prior **DevOps** experience is assumed/expected, |
| 5 | +however some understanding of basic Postgres concepts is _useful_. |
| 6 | + |
| 7 | +> **Note**: if you are new to Postgres or need a refresher, |
| 8 | +please see: |
| 9 | +[github.com/dwyl/**learn-postgresql**](https://github.com/dwyl/learn-postgresql) |
| 10 | + |
| 11 | + |
| 12 | +## 1. Create a VPS Instance |
| 13 | + |
| 14 | +If you don't already have a Virtual Private Server (VPS) instance, |
| 15 | +create one. |
| 16 | + |
| 17 | +> In the case of this guide, |
| 18 | +we are using DigitalOcean |
| 19 | +because they have a good balance of Price/Performance. |
| 20 | +> We did a bunch of research into the comparative cost/perf: |
| 21 | +[learn-devops/issues/58](https://github.com/dwyl/learn-devops/issues/58#issuecomment-660650080) |
| 22 | +We concluded that the **NVMe SSD Block Storage** of DigitalOcean, |
| 23 | +was the best in the cost/perf tradeoff. |
| 24 | + |
| 25 | +At the time of writing, |
| 26 | +there is no 1-click Postgres setup in the DO Marketplace: |
| 27 | +https://marketplace.digitalocean.com |
| 28 | +So we are doing our setup from scratch. |
| 29 | + |
| 30 | + |
| 31 | +Visit: |
| 32 | +Select the instance Operating System and Plan: |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +Select the instance Operating System and Plan: |
| 38 | + |
| 39 | + |
| 40 | +Scroll down to the "Add Block Storage" section and enter 1gb into the field: |
| 41 | + |
| 42 | + |
| 43 | +We're going to increase the size in a later step, so keep it low for now just to set it up. |
| 44 | +https://www.digitalocean.com/docs/volumes/how-to/increase-size/ |
| 45 | + |
| 46 | +Select `Ext4` for the filesystem and then select the datacenter region that bests suits your needs. |
| 47 | + |
| 48 | +> We selected `Ext4` filesystem based on reading this post: |
| 49 | +https://blog.pgaddict.com/posts/postgresql-performance-on-ext4-and-xfs |
| 50 | +It _appears_ to be 10% faster for Postgres. |
| 51 | + |
| 52 | +Scroll down until you see the "Choose a hostname" field. Enter a relevant name, in our case "hits": |
| 53 | + |
| 54 | + |
| 55 | +Scroll to the bottom of the page and click "**Create Droplet**". |
| 56 | + |
| 57 | +Given that the data directory will be on the _separate_ block storage, |
| 58 | +we aren't bothering with the $1/month backup. |
| 59 | + |
| 60 | +Once your server has been created, |
| 61 | +follow the initial server setup guide: |
| 62 | +https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04 |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +2. Install Postgres |
| 68 | + |
| 69 | +Following this guide: |
| 70 | +https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04 |
| 71 | + |
| 72 | +Login to the VPS using ssh: |
| 73 | +``` |
| 74 | + |
| 75 | +``` |
| 76 | + |
| 77 | +Update the OS and install Postgres: |
| 78 | + |
| 79 | +```sh |
| 80 | +sudo apt update |
| 81 | +sudo apt install postgresql postgresql-contrib |
| 82 | +``` |
| 83 | + |
| 84 | +Once everything is installed, |
| 85 | +Switch to the Postgres user: |
| 86 | + |
| 87 | +``` |
| 88 | +sudo -i -u postgres |
| 89 | +``` |
| 90 | + |
| 91 | +You should see the following in your terminal |
| 92 | +to indicate that you're logged in as the posgres user: |
| 93 | + |
| 94 | +``` |
| 95 | +postgres@hits:~$ |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | +``` |
| 100 | +createuser --interactive |
| 101 | +``` |
| 102 | + |
| 103 | +``` |
| 104 | +hitsnodelete |
| 105 | +``` |
| 106 | +This username is fairly selfevident. |
| 107 | +We want a user that can `insert` records but not `delete` any data. |
| 108 | + |
| 109 | +``` |
| 110 | +Shall the new role be a superuser? (y/n) |
| 111 | +``` |
| 112 | + |
| 113 | +Select `n`, for all options |
| 114 | +as we don't _want_ our user to be a `superuser`. |
| 115 | + |
| 116 | + |
| 117 | +## 3. Change Postgres Directory to use Block Storage |
| 118 | + |
| 119 | + |
| 120 | +Following this guide: |
| 121 | +https://www.digitalocean.com/community/tutorials/how-to-move-a-postgresql-data-directory-to-a-new-location-on-ubuntu-18-04 |
| 122 | + |
| 123 | + |
| 124 | +Login to postgres: |
| 125 | +``` |
| 126 | +sudo -u postgres psql |
| 127 | +``` |
| 128 | + |
| 129 | +Display the data directory: |
| 130 | +``` |
| 131 | +SHOW data_directory; |
| 132 | +``` |
| 133 | + |
| 134 | +You should see: |
| 135 | +``` |
| 136 | + data_directory |
| 137 | +----------------------------- |
| 138 | + /var/lib/postgresql/10/main |
| 139 | +(1 row) |
| 140 | +``` |
| 141 | + |
| 142 | +Quit postgres: |
| 143 | + |
| 144 | +``` |
| 145 | +\q |
| 146 | +``` |
| 147 | + |
| 148 | +Shut down postgres: |
| 149 | + |
| 150 | +``` |
| 151 | +sudo systemctl stop postgresql |
| 152 | +``` |
| 153 | + |
| 154 | +Confirm the status of postgres: |
| 155 | +``` |
| 156 | +sudo systemctl status postgresql |
| 157 | +``` |
| 158 | + |
| 159 | +You should see something similar to the following: |
| 160 | +``` |
| 161 | +● postgresql.service - PostgreSQL RDBMS |
| 162 | + Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) |
| 163 | + Active: inactive (dead) since Mon 2020-07-20 03:11:02 UTC; 7s ago |
| 164 | + Main PID: 8928 (code=exited, status=0/SUCCESS) |
| 165 | +
|
| 166 | +Jul 20 02:25:23 hits systemd[1]: Starting PostgreSQL RDBMS... |
| 167 | +Jul 20 02:25:23 hits systemd[1]: Started PostgreSQL RDBMS. |
| 168 | +Jul 20 03:11:02 hits systemd[1]: Stopped PostgreSQL RDBMS. |
| 169 | +``` |
| 170 | + |
| 171 | + |
| 172 | +Copy the postgres data directory to the block storage volume: |
| 173 | + |
| 174 | +``` |
| 175 | +sudo rsync -av /var/lib/postgresql /mnt/volume_lon1_01 |
| 176 | +``` |
| 177 | + |
| 178 | +In our case our mounted block storage is `/mnt/volume_lon1_01` |
| 179 | + |
| 180 | + |
| 181 | +Rename `main` data to `main.bak` just so we have a backup: |
| 182 | +``` |
| 183 | +sudo mv /var/lib/postgresql/10/main /var/lib/postgresql/10/main.bak |
| 184 | +``` |
| 185 | + |
| 186 | + |
| 187 | +Edit the postgres config: |
| 188 | + |
| 189 | +``` |
| 190 | +sudo nano /etc/postgresql/10/main/postgresql.conf |
| 191 | +``` |
| 192 | + |
| 193 | + |
| 194 | +Find the `data_directory` line and update it to: |
| 195 | +``` |
| 196 | +data_directory = '/mnt/volume_lon1_01/postgresql/10/main' |
| 197 | +``` |
| 198 | + |
| 199 | +Save and quit the file. |
| 200 | + |
| 201 | +Start postgres again: |
| 202 | + |
| 203 | +``` |
| 204 | +sudo systemctl start postgresql |
| 205 | +``` |
| 206 | + |
| 207 | +Check the status: |
| 208 | +``` |
| 209 | +sudo systemctl status postgresql |
| 210 | +``` |
| 211 | + |
| 212 | +You should see the following: |
| 213 | + |
| 214 | +``` |
| 215 | +● postgresql.service - PostgreSQL RDBMS |
| 216 | + Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) |
| 217 | + Active: active (exited) since Mon 2020-07-20 03:17:59 UTC; 5s ago |
| 218 | + Process: 20422 ExecStart=/bin/true (code=exited, status=0/SUCCESS) |
| 219 | + Main PID: 20422 (code=exited, status=0/SUCCESS) |
| 220 | +
|
| 221 | +Jul 20 03:17:59 hits systemd[1]: Starting PostgreSQL RDBMS... |
| 222 | +Jul 20 03:17:59 hits systemd[1]: Started PostgreSQL RDBMS. |
| 223 | +``` |
| 224 | + |
| 225 | +Login to `psql`: |
| 226 | + |
| 227 | +``` |
| 228 | +sudo -u postgres psql |
| 229 | +``` |
| 230 | + |
| 231 | +Show the `data_directory`: |
| 232 | + |
| 233 | +``` |
| 234 | +SHOW data_directory; |
| 235 | +``` |
| 236 | + |
| 237 | +You should see: |
| 238 | + |
| 239 | +``` |
| 240 | + data_directory |
| 241 | +---------------------------------------- |
| 242 | + /mnt/volume_lon1_01/postgresql/10/main |
| 243 | +(1 row) |
| 244 | +``` |
| 245 | + |
| 246 | +Success. |
0 commit comments