Skip to content

Commit 530f896

Browse files
bagasmeguillep2klunnyzeripathsapk
authored
[Docs] Database Preparation (#9826)
* Database Preparation * Apply suggestions from @guillep2k Co-Authored-By: guillep2k <[email protected]> * Repeat strong password notice on PostgreSQL * Add prerequisite note * Use utf8mb4 instead of utf8 * Use utf8mb4 if complete character coverage wanted * utf8mb4 recommended * Reword utf8mb4 recommendation * Set InnoDB global variables may be needed * Reorder step number * Whoops, bump year date * Remove `SET GLOBAL`s and step number reorder It is assumed that such global variables value are already as default on MySQL 5.7 and later, so those are redundant. * Syntax edit on MySQL GRANT PRIVILEGEs * DB engine uniformity intro edit Co-authored-by: guillep2k <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: Antoine GIRARD <[email protected]>
1 parent a6dc8f7 commit 530f896

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
date: "2020-01-16"
3+
title: "Database Preparation"
4+
slug: "database-prep"
5+
weight: 10
6+
toc: true
7+
draft: false
8+
menu:
9+
sidebar:
10+
parent: "installation"
11+
name: "Database preparation"
12+
weight: 20
13+
identifier: "database-prep"
14+
---
15+
16+
You need a database to use Gitea. Gitea supports PostgreSQL, MySQL, SQLite, and MSSQL. This page will guide into preparing database. Only PostgreSQL and MySQL will be covered here since those database engines are widely-used in production.
17+
18+
Database instance can be on same machine as Gitea (local database setup), or on different machine (remote database).
19+
20+
Note: All steps below requires that the database engine of your choice is installed on your system. For remote database setup, install the server part on database instance and client part on your Gitea server. 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.
21+
22+
## MySQL
23+
24+
1. On database instance, login to database console as root:
25+
26+
```
27+
mysql -u root -p
28+
```
29+
30+
Enter the password as prompted.
31+
32+
2. 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.
33+
34+
For local database:
35+
36+
```sql
37+
SET old_passwords=0;
38+
CREATE USER 'gitea' IDENTIFIED BY 'gitea';
39+
```
40+
41+
For remote database:
42+
43+
```sql
44+
SET old_passwords=0;
45+
CREATE USER 'gitea'@'12.34.56.78' IDENTIFIED BY 'gitea';
46+
```
47+
48+
where `12.34.56.78` is the IP address of your Gitea instance.
49+
50+
Replace username and password above as appropriate.
51+
52+
3. Create database with UTF-8 charset and collation. Make sure to use `utf8mb4` charset instead of `utf8` as the former supports all Unicode characters (including emojis) beyond *Basic Multilingual Plane*. Also, collation chosen depending on your expected content. When in doubt, use either `unicode_ci` or `general_ci`.
53+
54+
```sql
55+
CREATE DATABASE 'giteadb' CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
56+
```
57+
58+
Replace database name as appropriate.
59+
60+
4. Grant all privileges on the database to database user created above.
61+
62+
For local database:
63+
64+
```sql
65+
GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea';
66+
FLUSH PRIVILEGES;
67+
```
68+
69+
For remote database:
70+
71+
```sql
72+
GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'@'12.34.56.78';
73+
FLUSH PRIVILEGES;
74+
```
75+
76+
5. Quit from database console by `exit`.
77+
78+
6. On your Gitea server, test connection to the database:
79+
80+
```
81+
mysql -u gitea -h 23.45.67.89 -p giteadb
82+
```
83+
84+
where `gitea` is database username, `giteadb` is database name, and `23.45.67.89` is IP address of database instance. Omit `-h` option for local database.
85+
86+
You should be connected to the database.
87+
88+
## PostgreSQL
89+
90+
1. 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:
91+
92+
```ini
93+
password_encryption = scram-sha-256
94+
```
95+
96+
Restart PostgreSQL to apply the setting.
97+
98+
2. On the database server, login to the database console as superuser:
99+
100+
```
101+
su -c "psql" - postgres
102+
```
103+
104+
3. Create database user (role in PostgreSQL terms) with login privilege and password. Please use a secure, strong password instead of `'gitea'` below:
105+
106+
```sql
107+
CREATE ROLE gitea WITH LOGIN PASSWORD 'gitea';
108+
```
109+
110+
Replace username and password as appropriate.
111+
112+
4. 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:
113+
114+
```sql
115+
CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';
116+
```
117+
118+
Replace database name as appropriate.
119+
120+
5. Allow the database user to access the database created above by adding the following authentication rule to `pg_hba.conf`.
121+
122+
For local database:
123+
124+
```ini
125+
local giteadb gitea scram-sha-256
126+
```
127+
128+
For remote database:
129+
130+
```ini
131+
host giteadb gitea 12.34.56.78/32 scram-sha-256
132+
```
133+
134+
Replace database name, user, and IP address of Gitea instance with your own.
135+
136+
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.
137+
138+
Restart PostgreSQL to apply new authentication rules.
139+
140+
6. On your Gitea server, test connection to the database.
141+
142+
For local database:
143+
144+
```
145+
psql -U gitea -d giteadb
146+
```
147+
148+
For remote database:
149+
150+
```
151+
psql "postgres://[email protected]/giteadb"
152+
```
153+
154+
where `gitea` is database user, `giteadb` is database name, and `23.45.67.89` is IP address of your database instance.
155+
156+
You should be prompted to enter password for the database user, and connected to the database.

0 commit comments

Comments
 (0)