code.gitea.io/gitea@v1.22.3/docs/content/installation/database-preparation.en-us.md (about) 1 --- 2 date: "2020-01-16" 3 title: "Database Preparation" 4 slug: "database-prep" 5 sidebar_position: 10 6 toc: false 7 draft: false 8 aliases: 9 - /en-us/database-prep 10 menu: 11 sidebar: 12 parent: "installation" 13 name: "Database preparation" 14 sidebar_position: 10 15 identifier: "database-prep" 16 --- 17 18 # Database Preparation 19 20 You need a database to use Gitea. Gitea supports PostgreSQL (>= 12), MySQL (>= 8.0), MariaDB (>= 10.4), SQLite (builtin), and MSSQL (>= 2012 SP4). This page will guide into preparing database. Only PostgreSQL and MySQL will be covered here since those database engines are widely-used in production. If you plan to use SQLite, you can ignore this chapter. 21 22 If you use an unsupported database version, please [get in touch](/help/support) with us for information on our Extended Support Contracts. We can provide testing and support for older databases and integrate those fixes into the Gitea codebase. 23 24 Database instance can be on same machine as Gitea (local database setup), or on different machine (remote database). 25 26 Note: All steps below requires that the database engine of your choice is installed on your system. For remote database setup, install the server application on database instance and client program on your Gitea server. The client program is used to test connection to the database from Gitea server, while Gitea itself use database driver provided by Go to accomplish the same thing. In addition, make sure you use same engine version for both server and client for some engine features to work. For security reason, protect `root` (MySQL) or `postgres` (PostgreSQL) database superuser with secure password. The steps assumes that you run Linux for both database and Gitea servers. 27 28 ## MySQL/MariaDB 29 30 1. For remote database setup, you will need to make MySQL listen to your IP address. Edit `bind-address` option on `/etc/mysql/my.cnf` on database instance to: 31 32 ```ini 33 bind-address = 203.0.113.3 34 ``` 35 36 2. On database instance, login to database console as root: 37 38 ``` 39 mysql -u root -p 40 ``` 41 42 Enter the password as prompted. 43 44 3. Create database user which will be used by Gitea, authenticated by password. This example uses `'gitea'` as password. Please use a secure password for your instance. 45 46 For local database: 47 48 ```sql 49 SET old_passwords=0; 50 CREATE USER 'gitea'@'%' IDENTIFIED BY 'gitea'; 51 ``` 52 53 For remote database: 54 55 ```sql 56 SET old_passwords=0; 57 CREATE USER 'gitea'@'192.0.2.10' IDENTIFIED BY 'gitea'; 58 ``` 59 60 where `192.0.2.10` is the IP address of your Gitea instance. 61 62 Replace username and password above as appropriate. 63 64 4. Create database with UTF-8 charset and case-sensitive collation. 65 66 `utf8mb4_bin` is a common collation for both MySQL/MariaDB. 67 When Gitea starts, it will try to find a better collation (`utf8mb4_0900_as_cs` or `uca1400_as_cs`) and alter the database if it is possible. 68 If you would like to use other collation, you can set `[database].CHARSET_COLLATION` in the `app.ini` file. 69 70 ```sql 71 CREATE DATABASE giteadb CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin'; 72 ``` 73 74 Replace database name as appropriate. 75 76 5. Grant all privileges on the database to database user created above. 77 78 For local database: 79 80 ```sql 81 GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'; 82 FLUSH PRIVILEGES; 83 ``` 84 85 For remote database: 86 87 ```sql 88 GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'@'192.0.2.10'; 89 FLUSH PRIVILEGES; 90 ``` 91 92 6. Quit from database console by `exit`. 93 94 7. On your Gitea server, test connection to the database: 95 96 ``` 97 mysql -u gitea -h 203.0.113.3 -p giteadb 98 ``` 99 100 where `gitea` is database username, `giteadb` is database name, and `203.0.113.3` is IP address of database instance. Omit `-h` option for local database. 101 102 You should be connected to the database. 103 104 ## PostgreSQL 105 106 1. For remote database setup, configure PostgreSQL on database instance to listen to your IP address by editing `listen_addresses` on `postgresql.conf` to: 107 108 ```ini 109 listen_addresses = 'localhost, 203.0.113.3' 110 ``` 111 112 2. PostgreSQL uses `md5` challenge-response encryption scheme for password authentication by default. Nowadays this scheme is not considered secure anymore. Use SCRAM-SHA-256 scheme instead by editing the `postgresql.conf` configuration file on the database server to: 113 114 ```ini 115 password_encryption = scram-sha-256 116 ``` 117 118 Restart PostgreSQL to apply the setting. 119 120 3. On the database server, login to the database console as superuser: 121 122 ``` 123 su -c "psql" - postgres 124 ``` 125 126 4. Create database user (role in PostgreSQL terms) with login privilege and password. Please use a secure, strong password instead of `'gitea'` below: 127 128 ```sql 129 CREATE ROLE gitea WITH LOGIN PASSWORD 'gitea'; 130 ``` 131 132 Replace username and password as appropriate. 133 134 5. Create database with UTF-8 charset and owned by the database user created earlier. Any `libc` collations can be specified with `LC_COLLATE` and `LC_CTYPE` parameter, depending on expected content: 135 136 ```sql 137 CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8'; 138 ``` 139 140 Replace database name as appropriate. 141 142 6. Allow the database user to access the database created above by adding the following authentication rule to `pg_hba.conf`. 143 144 For local database: 145 146 ```ini 147 local giteadb gitea scram-sha-256 148 ``` 149 150 For remote database: 151 152 ```ini 153 host giteadb gitea 192.0.2.10/32 scram-sha-256 154 ``` 155 156 Replace database name, user, and IP address of Gitea instance with your own. 157 158 Note: rules on `pg_hba.conf` are evaluated sequentially, that is the first matching rule will be used for authentication. Your PostgreSQL installation may come with generic authentication rules that match all users and databases. You may need to place the rules presented here above such generic rules if it is the case. 159 160 Restart PostgreSQL to apply new authentication rules. 161 162 7. On your Gitea server, test connection to the database. 163 164 For local database: 165 166 ``` 167 psql -U gitea -d giteadb 168 ``` 169 170 For remote database: 171 172 ``` 173 psql "postgres://gitea@203.0.113.3/giteadb" 174 ``` 175 176 where `gitea` is database user, `giteadb` is database name, and `203.0.113.3` is IP address of your database instance. 177 178 You should be prompted to enter password for the database user, and connected to the database. 179 180 ## Database Connection over TLS 181 182 If the communication between Gitea and your database instance is performed through a private network, or if Gitea and the database are running on the same server, this section can be omitted since the security between Gitea and the database instance is not critically exposed. If instead the database instance is on a public network, use TLS to encrypt the connection to the database, as it is possible for third-parties to intercept the traffic data. 183 184 ### Prerequisites 185 186 - You need two valid TLS certificates, one for the database instance (database server) and one for the Gitea instance (database client). Both certificates must be signed by a trusted CA. 187 - The database certificate must contain `TLS Web Server Authentication` in the `X509v3 Extended Key Usage` extension attribute, while the client certificate needs `TLS Web Client Authentication` in the corresponding attribute. 188 - On the database server certificate, one of `Subject Alternative Name` or `Common Name` entries must be the fully-qualified domain name (FQDN) of the database instance (e.g. `db.example.com`). On the database client certificate, one of the entries mentioned above must contain the database username that Gitea will be using to connect. 189 - You need domain name mappings of both Gitea and database servers to their respective IP addresses. Either set up DNS records for them or add local mappings to `/etc/hosts` (`%WINDIR%\System32\drivers\etc\hosts` in Windows) on each system. This allows the database connections to be performed by domain name instead of IP address. See documentation of your system for details. 190 191 ### PostgreSQL TLS 192 193 The PostgreSQL driver used by Gitea supports two-way TLS. In two-way TLS, both database client and server authenticate each other by sending their respective certificates to their respective opposite for validation. In other words, the server verifies client certificate, and the client verifies server certificate. 194 195 1. On the server with the database instance, place the following credentials: 196 197 - `/path/to/postgresql.crt`: Database instance certificate 198 - `/path/to/postgresql.key`: Database instance private key 199 - `/path/to/root.crt`: CA certificate chain to validate client certificates 200 201 2. Add following options to `postgresql.conf`: 202 203 ```ini 204 ssl = on 205 ssl_ca_file = '/path/to/root.crt' 206 ssl_cert_file = '/path/to/postgresql.crt' 207 ssl_key_file = '/path/to/postgresql.key' 208 ssl_min_protocol_version = 'TLSv1.2' 209 ``` 210 211 3. Adjust credentials ownership and permission, as required by PostgreSQL: 212 213 ``` 214 chown postgres:postgres /path/to/root.crt /path/to/postgresql.crt /path/to/postgresql.key 215 chmod 0600 /path/to/root.crt /path/to/postgresql.crt /path/to/postgresql.key 216 ``` 217 218 4. Edit `pg_hba.conf` rule to only allow Gitea database user to connect over SSL, and to require client certificate verification. 219 220 For PostgreSQL 12: 221 222 ```ini 223 hostssl giteadb gitea 192.0.2.10/32 scram-sha-256 clientcert=verify-full 224 ``` 225 226 For PostgreSQL 11 and earlier: 227 228 ```ini 229 hostssl giteadb gitea 192.0.2.10/32 scram-sha-256 clientcert=1 230 ``` 231 232 Replace database name, user, and IP address of Gitea instance as appropriate. 233 234 5. Restart PostgreSQL to apply configurations above. 235 236 6. On the server running the Gitea instance, place the following credentials under the home directory of the user who runs Gitea (e.g. `git`): 237 238 - `~/.postgresql/postgresql.crt`: Database client certificate 239 - `~/.postgresql/postgresql.key`: Database client private key 240 - `~/.postgresql/root.crt`: CA certificate chain to validate server certificate 241 242 Note: Those file names above are hardcoded in PostgreSQL and it is not possible to change them. 243 244 7. Adjust credentials, ownership and permission as required: 245 246 ``` 247 chown git:git ~/.postgresql/postgresql.crt ~/.postgresql/postgresql.key ~/.postgresql/root.crt 248 chown 0600 ~/.postgresql/postgresql.crt ~/.postgresql/postgresql.key ~/.postgresql/root.crt 249 ``` 250 251 8. Test the connection to the database: 252 253 ``` 254 psql "postgres://gitea@example.db/giteadb?sslmode=verify-full" 255 ``` 256 257 You should be prompted to enter password for the database user, and then be connected to the database. 258 259 ### MySQL/MariaDB TLS 260 261 While the MySQL driver used by Gitea also supports two-way TLS, Gitea currently supports only one-way TLS. See issue #10828 for details. 262 263 In one-way TLS, the database client verifies the certificate sent from server during the connection handshake, and the server assumes that the connected client is legitimate, since client certificate verification doesn't take place. 264 265 1. On the database instance, place the following credentials: 266 267 - `/path/to/mysql.crt`: Database instance certificate 268 - `/path/to/mysql.key`: Database instance key 269 - `/path/to/ca.crt`: CA certificate chain. This file isn't used on one-way TLS, but is used to validate client certificates on two-way TLS. 270 271 2. Add following options to `my.cnf`: 272 273 ```ini 274 [mysqld] 275 ssl-ca = /path/to/ca.crt 276 ssl-cert = /path/to/mysql.crt 277 ssl-key = /path/to/mysql.key 278 tls-version = TLSv1.2,TLSv1.3 279 ``` 280 281 3. Adjust credentials ownership and permission: 282 283 ``` 284 chown mysql:mysql /path/to/ca.crt /path/to/mysql.crt /path/to/mysql.key 285 chmod 0600 /path/to/ca.crt /path/to/mysql.crt /path/to/mysql.key 286 ``` 287 288 4. Restart MySQL to apply the setting. 289 290 5. The database user for Gitea may have been created earlier, but it would authenticate only against the IP addresses of the server running Gitea. To authenticate against its domain name, recreate the user, and this time also set it to require TLS for connecting to the database: 291 292 ```sql 293 DROP USER 'gitea'@'192.0.2.10'; 294 CREATE USER 'gitea'@'example.gitea' IDENTIFIED BY 'gitea' REQUIRE SSL; 295 GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'@'example.gitea'; 296 FLUSH PRIVILEGES; 297 ``` 298 299 Replace database user name, password, and Gitea instance domain as appropriate. 300 301 6. Make sure that the CA certificate chain required to validate the database server certificate is on the system certificate store of both the database and Gitea servers. Consult your system documentation for instructions on adding a CA certificate to the certificate store. 302 303 7. On the server running Gitea, test connection to the database: 304 305 ``` 306 mysql -u gitea -h example.db -p --ssl 307 ``` 308 309 You should be connected to the database.