Database Preparation
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.
If you use an unsupported database version, please get in touch 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.
Database instance can be on same machine as Gitea (local database setup), or on different machine (remote database).
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.
MySQL/MariaDB
-
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:bind-address = 203.0.113.3
-
On database instance, login to database console as root:
mysql -u root -p
Enter the password as prompted.
-
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.For local database:
SET old_passwords=0;
CREATE USER 'gitea'@'%' IDENTIFIED BY 'gitea';For remote database:
SET old_passwords=0;
CREATE USER 'gitea'@'192.0.2.10' IDENTIFIED BY 'gitea';where
192.0.2.10
is the IP address of your Gitea instance.Replace username and password above as appropriate.
-
Create database with UTF-8 charset and case-sensitive collation.
utf8mb4_bin
is a common collation for both MySQL/MariaDB. When Gitea starts, it will try to find a better collation (utf8mb4_0900_as_cs
oruca1400_as_cs
) and alter the database if it is possible. If you would like to use other collation, you can set[database].CHARSET_COLLATION
in theapp.ini
file.CREATE DATABASE giteadb CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';
Replace database name as appropriate.
-
Grant all privileges on the database to database user created above.
For local database:
GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea';
FLUSH PRIVILEGES;For remote database:
GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'@'192.0.2.10';
FLUSH PRIVILEGES; -
Quit from database console by
exit
. -
On your Gitea server, test connection to the database:
mysql -u gitea -h 203.0.113.3 -p giteadb
where
gitea
is database username,giteadb
is database name, and203.0.113.3
is IP address of database instance. Omit-h
option for local database.You should be connected to the database.
PostgreSQL
-
For remote database setup, configure PostgreSQL on database instance to listen to your IP address by editing
listen_addresses
onpostgresql.conf
to:listen_addresses = 'localhost, 203.0.113.3'
-
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 thepostgresql.conf
configuration file on the database server to:password_encryption = scram-sha-256
Restart PostgreSQL to apply the setting.
-
On the database server, login to the database console as superuser:
su -c "psql" - postgres
-
Create database user (role in PostgreSQL terms) with login privilege and password. Please use a secure, strong password instead of
'gitea'
below:CREATE ROLE gitea WITH LOGIN PASSWORD 'gitea';
Replace username and password as appropriate.
-
Create database with UTF-8 charset and owned by the database user created earlier. Any
libc
collations can be specified withLC_COLLATE
andLC_CTYPE
parameter, depending on expected content:CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';
Replace database name as appropriate.
-
Allow the database user to access the database created above by adding the following authentication rule to
pg_hba.conf
.For local database:
local giteadb gitea scram-sha-256
For remote database:
host giteadb gitea 192.0.2.10/32 scram-sha-256
Replace database name, user, and IP address of Gitea instance with your own.
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.Restart PostgreSQL to apply new authentication rules.
-
On your Gitea server, test connection to the database.
For local database:
psql -U gitea -d giteadb
For remote database:
psql "postgres://gitea@203.0.113.3/giteadb"
where
gitea
is database user,giteadb
is database name, and203.0.113.3
is IP address of your database instance.You should be prompted to enter password for the database user, and connected to the database.
Database Connection over TLS
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.