# Database Drivers

By default, simple-auth stores all credentials as a sqlite3 database named simpleauth.db in the working directory of the app. In docker the docker file, that may be in a slightly different spot (See docker docs)

In some cases, you may want to change that. For instance, postgres often performs better than sqlite. Or, you might want your persistence in a different spot.

# Configuring Simple-Auth

This is the applicable section of the configuration file (with its defaults)

db:
    driver: "sqlite3"     # Storage driver: "sqlite3", "postgres", "mysql"
    url: "simpleauth.db"  # Storage connection URL. See http://gorm.io/docs/connecting_to_the_database.html

In order to use different persistence, you can chose a different driver and url.

# Drivers

# Sqlite

This is the default. To sepecify a different path, change the db.url config.

# Docker

If using docker, make sure to mount a persistent volume, otherwise you may lose your users on container restart.

simple-auth will put all its persistent storage here: /var/lib/simple-auth

version: '3.3'
services:
  simpleauth:
    image: zix99/simple-auth:latest
    ports:
      - 8082:80
    environment:
      SA_WEB_LOGIN_COOKIE_JWT_SIGNINGKEY: a-unqiue-signing-key # REPLACE THIS WITH SOMETHING UNIQUE!!!!
    volumes:
      - sadb:/var/lib/simple-auth

volumes:
  sadb: {}

# Sqlite in-memory

WARNING

You will lose all data upon application termination

It can sometimes be useful to have a temporary in-memory database (mainly for testing or integration testing). For this you can use the sqlite3 driver, with the url file::memory:?cache=shared like so:

db:
  url: "file::memory:?cache=shared"

# Postgres

This is an example setup using a docker-compose file to specify the config for a postgres instance.

If you use a standalone applicable, you only need to set SA_DB_DRIVER and SA_DB_URL

SA_DB_DRIVER: postgres
SA_DB_URL: "host=db user=postgres dbname=postgres password=test sslmode=disable"

# Docker-Compose

version: '3.3'
services:
  simpleauth:
    image: zix99/simple-auth:latest
    ports:
      - 8082:80
    environment:
      SA_WEB_LOGIN_COOKIE_JWT_SIGNINGKEY: a-unqiue-signing-key # REPLACE THIS WITH SOMETHING UNIQUE!!!!
      #region pgconfig
      SA_DB_DRIVER: postgres
      SA_DB_URL: "host=db user=postgres dbname=postgres password=test sslmode=disable"
      #endregion pgconfig
    depends_on:
      - db
    restart: always
  db:
    image: postgres:13-alpine
    environment:
      POSTGRES_PASSWORD: test
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata: {}

# Mysql

WARNING

Make sure to set ?charset=utf8&parseTime=True&loc=Local on the connection, otherwise you will receive errors

SA_DB_DRIVER: mysql
SA_DB_URL: "sa:test@(db)/simpleauth?charset=utf8&parseTime=True&loc=Local"

# Docker-Compose

version: '3.3'
services:
  simpleauth:
    image: zix99/simple-auth:latest
    ports:
      - 8082:80
    environment:
      SA_WEB_LOGIN_COOKIE_JWT_SIGNINGKEY: a-unqiue-signing-key # REPLACE THIS WITH SOMETHING UNIQUE!!!!
      #region myconfig
      SA_DB_DRIVER: mysql
      SA_DB_URL: "sa:test@(db)/simpleauth?charset=utf8&parseTime=True&loc=Local"
      #endregion myconfig
    restart: always
    depends_on:
      - db
  db:
    image: mariadb:10.5
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
      MYSQL_USER: sa
      MYSQL_PASSWORD: test
      MYSQL_DATABASE: simpleauth
    volumes:
      - dbdata:/var/lib/mysql

volumes:
  dbdata: {}