Database
Choose between PostgreSQL, Turso with Embedded Replicas, or SQLite
Configuration
This choice can be made using the GoFast CLI or by modifying the docker-compose.yml
file.
via CLI
Possible options are SQLite
, Turso with Embedded Replicas
, PostgreSQL (local)
and PostgreSQL (remote)
.
The PostgreSQL (local)
will add a PostgreSQL instance to your Docker Compose file.
via Docker Compose
Possible options are sqlite
, turso
, postgres
and memory
.
Database Configuration
Depending on the provider, you need to set the following environment variables in the docker-compose.yml
file:
- for
sqlite
, setSQLITE_FILE
to the file location. - for
turso
, setTURSO_URL
andTURSO_TOKEN
. - for
postgres
, setPOSTGRES_HOST
,POSTGRES_PORT
,POSTGRES_DB
,POSTGRES_USER
andPOSTGRES_PASS
. - for
memory
, no configuration is needed.
services:
server:
environment:
DB_PROVIDER: sqlite
SQLITE_FILE: /server/storage/local.db
# DB_PROVIDER: turso
# TURSO_URL: ${TURSO_URL}
# TURSO_TOKEN: ${TURSO_TOKEN}
# DB_PROVIDER: postgres
# POSTGRES_HOST: ${POSTGRES_HOST}
# POSTGRES_PORT: ${POSTGRES_PORT}
# POSTGRES_DB: ${POSTGRES_DB}
# POSTGRES_USER: ${POSTGRES_USER}
# POSTGRES_PASS: ${POSTGRES_PASS}
Implementation Details
SQLite
The only environment variable required here is SQLITE_FILE
, which points to the file location of the database.
services:
server:
environment:
DB_PROVIDER: sqlite
SQLITE_FILE: /server/storage/local.db
This is also the default configuration when building the base project, with a modern setup:
var pragmas = []string{
"PRAGMA journal_mode = WAL;",
"PRAGMA foreign_keys = OFF;",
"PRAGMA synchronous = NORMAL;",
"PRAGMA temp_store = MEMORY;",
"PRAGMA auto_vacuum = INCREMENTAL;",
"PRAGMA cache_size = 10000;",
"PRAGMA cache = shared;",
}
This is more than enough for 90% of applications. For backups, you can use:
sqlite3 local.db '.backup local.db.bak'
PostgreSQL
When using the CLI, you can choose between a local or remote PostgreSQL instance. The local option will add a PostgreSQL instance to your Docker Compose file:
services:
server:
environment:
DB_PROVIDER: postgres
# it uses the internal network to connect to the database
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_DB: gofast
POSTGRES_USER: gofast
POSTGRES_PASS: gofast
postgres:
image: postgres
environment:
POSTGRES_DB: gofast
POSTGRES_USER: gofast
POSTGRES_PASSWORD: gofast
ports:
- 5432:5432
For the remote option, you need to set the following environment variables:
services:
server:
environment:
DB_PROVIDER: postgres
POSTGRES_HOST: ${POSTGRES_HOST}
POSTGRES_PORT: ${POSTGRES_PORT}
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASS: ${POSTGRES_PASS}
Turso with Embedded Replicas
For Turso, first log in and create a database:
You need two environment variables, TURSO_URL
and TURSO_TOKEN
, both of which can be found here:
After setting up the database, you can use the Turso provider:
services:
server:
environment:
DB_PROVIDER: turso
TURSO_URL: ${TURSO_URL}
TURSO_TOKEN: ${TURSO_TOKEN}
Adding a new provider
To add a new provider, follow these steps in the /storage.go
file:
- Add the new provider to the
Provider
constant.
const (
Sqlite Provider = "sqlite"
Turso Provider = "turso"
Postgres Provider = "postgres"
Memory Provider = "memory"
MySQL Provider = "mysql"
)
- Create a new provider function that returns database driver and cleanup functions.
func newMySQLStorage() (*Storage, error, func()) {
// Implement the MySQL connection here
}
- Return the new provider in the
NewStorage
function:
case MySQL:
return newMySQLStorage()
-
Add any new secrets in the
env.go
file. -
Fill in the
docker-compose.yml
file with the new provider configuration.
services:
server:
environment:
DB_PROVIDER: mysql
MYSQL_HOST: ${MYSQL_HOST}
MYSQL_PORT: ${MYSQL_PORT}
MYSQL_DB: ${MYSQL_DB}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASS: ${MYSQL_PASS}
Troubleshooting
- When running Docker on WSL2, the SQLite file may not work. In that case, you probably need to exclude it from volumes:
volumes:
- ./go:/server
- /server/storage/db/
environment:
DB_PROVIDER: sqlite
SQLITE_FILE: /server/storage/db/local.db
Need help?
Visit our discord server to ask any questions, make suggestions and give feedback :).