|
| 1 | +# PostgreSQL |
| 2 | + |
| 3 | +Coder ships with a built-in PostgreSQL database, but if you'd like to set up and |
| 4 | +use your own, refer to the following instructions. |
| 5 | + |
| 6 | +For in-depth information, please see the [PostgreSQL |
| 7 | +documentation](https://www.postgresql.org/docs/current/tutorial-start.html). |
| 8 | + |
| 9 | +## Step 1: Install and start PostgreSQL |
| 10 | + |
| 11 | +### macOS with Homebrew |
| 12 | + |
| 13 | +1. Install with Homebrew: |
| 14 | + |
| 15 | + ```console |
| 16 | + brew install postgres |
| 17 | + ``` |
| 18 | + |
| 19 | +1. Start PostgreSQL with brew |
| 20 | + |
| 21 | + ```console |
| 22 | + brew services start postgresql |
| 23 | + ``` |
| 24 | + |
| 25 | +1. Connect to PostgreSQL: |
| 26 | + |
| 27 | + ```console |
| 28 | + psql postgres |
| 29 | + ``` |
| 30 | + |
| 31 | +### Debian/Ubuntu |
| 32 | + |
| 33 | +1. Install PostgreSQL: |
| 34 | + |
| 35 | + ```console |
| 36 | + sudo apt-get install -y postgresql |
| 37 | + ``` |
| 38 | + |
| 39 | +1. Start PostgreSQL: |
| 40 | + |
| 41 | + ```console |
| 42 | + sudo systemctl enable postgresql |
| 43 | + sudo systemctl start postgresql |
| 44 | + ``` |
| 45 | + |
| 46 | +1. Connect to PostgreSQL: |
| 47 | + |
| 48 | + ```console |
| 49 | + sudo -u postgresql psql |
| 50 | + ``` |
| 51 | + |
| 52 | +## Step 2: Create a database and user for Coder |
| 53 | + |
| 54 | +1. Create the `coderuser` role: |
| 55 | + |
| 56 | + ```console |
| 57 | + create role coderuser with login; |
| 58 | + ``` |
| 59 | + |
| 60 | +1. Create a database called `coder` and assign the owner: |
| 61 | + |
| 62 | + ```console |
| 63 | + create database coder owner coder; |
| 64 | + ``` |
| 65 | + |
| 66 | +1. Set the password for `coderuser`: |
| 67 | + |
| 68 | + ```console |
| 69 | + \password coder # enter password when prompted |
| 70 | + ``` |
| 71 | + |
| 72 | +1. Assign rights to the database to your user: |
| 73 | + |
| 74 | + ```console |
| 75 | + grant all privileges on database coder to coderuser; |
| 76 | + ``` |
| 77 | + |
| 78 | +## Using your PostgreSQL database with Coder |
| 79 | + |
| 80 | +To use your Postgres database with Coder, provide the `CODER_PG_CONNECTION_URL` |
| 81 | +variable: |
| 82 | + |
| 83 | +```console |
| 84 | +postgresql://[user[:password]@][networkLocation][:port][/dbname][?param1=value1&...] |
| 85 | +``` |
| 86 | + |
| 87 | +Append to `coder server` to start your deployment. For example: |
| 88 | + |
| 89 | +```console |
| 90 | +CODER_PG_CONNECTION_URL="postgres://<databaseUsername>@0.0.0.0/<databaseName>?sslmode=disable&password=<password>" \ |
| 91 | + coder server -a 0.0.0.0:3000 --verbose |
| 92 | +``` |
| 93 | + |
| 94 | +> If you [installed Coder manually](install.md), you can add the `CODER_PG_CONNECTION_URL` |
| 95 | +variable to `/etc/coder.d/coder.env`. |
0 commit comments