From 13e05169619795f386aa36ded48dfdf4b74194f2 Mon Sep 17 00:00:00 2001 From: tomamplius Date: Thu, 12 Jan 2023 21:00:58 +0100 Subject: [PATCH] Add postgres as database engine (#388) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add postgres as database engine * Fix sissbruecker review * replace psycopg2 by psycopg2-binary * Fix Docker setup * Polish docs Co-authored-by: Sascha Ißbrücker --- .env.sample | 18 +++++++++++++++++ Dockerfile | 4 ++-- README.md | 6 +++++- docs/Options.md | 41 +++++++++++++++++++++++++++++++++++++++ requirements.prod.txt | 1 + requirements.txt | 1 + siteroot/settings/base.py | 38 ++++++++++++++++++++++++++---------- 7 files changed, 96 insertions(+), 13 deletions(-) diff --git a/.env.sample b/.env.sample index 0625618..d13947c 100644 --- a/.env.sample +++ b/.env.sample @@ -27,3 +27,21 @@ LD_AUTH_PROXY_LOGOUT_URL= # List of trusted origins from which to accept POST requests # See docs/Options.md for more details LD_CSRF_TRUSTED_ORIGINS= + +# Database settings +# These are currently only required for configuring PostreSQL. +# By default, linkding uses SQLite for which you don't need to configure anything. + +# Database engine, can be sqlite (default) or postgres +LD_DB_ENGINE= +# Database name (default: linkding) +LD_DB_DATABASE= +# Username to connect to the database server (default: linkding) +LD_DB_USER= +# Password to connect to the database server +LD_DB_PASSWORD= +# The hostname where the database is hosted (default: localhost) +LD_DB_HOST= +# Port use to connect to the database server +# Should use the default port if not set +LD_DB_PORT= diff --git a/Dockerfile b/Dockerfile index fc2569b..1d49054 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ RUN npm run build FROM python:3.10.6-slim-buster AS python-base -RUN apt-get update && apt-get -y install build-essential +RUN apt-get update && apt-get -y install build-essential libpq-dev WORKDIR /etc/linkding @@ -34,7 +34,7 @@ RUN mkdir /opt/venv && \ FROM python:3.10.6-slim-buster as final -RUN apt-get update && apt-get -y install mime-support +RUN apt-get update && apt-get -y install mime-support libpq-dev WORKDIR /etc/linkding # copy prod dependencies COPY --from=prod-deps /opt/venv /opt/venv diff --git a/README.md b/README.md index 28a9236..0f34b57 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,11 @@ The name comes from: ## Installation -linkding is designed to be run with container solutions like [Docker](https://docs.docker.com/get-started/). The Docker image is compatible with ARM platforms, so it can be run on a Raspberry Pi. +linkding is designed to be run with container solutions like [Docker](https://docs.docker.com/get-started/). +The Docker image is compatible with ARM platforms, so it can be run on a Raspberry Pi. + +By default, linkding uses SQLite as a database. +Alternatively linkding supports PostgreSQL, see the [database options](docs/Options.md#LD_DB_ENGINE) for more information. ### Using Docker diff --git a/docs/Options.md b/docs/Options.md index 0b9f53d..008cb30 100644 --- a/docs/Options.md +++ b/docs/Options.md @@ -108,3 +108,44 @@ Note that the setting **must** include the correct protocol (`https` or `http`), Multiple origins can be specified by separating them with a comma (`,`). This setting is adopted from the Django framework used by linkding, more information on the setting is available in the [Django documentation](https://docs.djangoproject.com/en/4.0/ref/settings/#std-setting-CSRF_TRUSTED_ORIGINS). + +### `LD_DB_ENGINE` + +Values: `postgres` or `sqlite` | Default = `sqlite` + +Database engine used by linkding to store data. +Currently, linkding supports SQLite and PostgreSQL. +By default, linkding uses SQLite, for which you don't need to configure anything. +All the other database variables below are only required for configured PostgresSQL. + +### `LD_DB_DATABASE` + +Values: `String` | Default = `linkding` + +The name of the database. + +### `LD_DB_USER` + +Values: `String` | Default = `linkding` + +The name of the user to connect to the database server. + +### `LD_DB_PASSWORD` + +Values: `String` | Default = None + +The password of the user to connect to the database server. +The password must be configured when using a database other than SQLite, there is no default value. + +### `LD_DB_HOST` + +Values: `String` | Default = `localhost` + +The hostname or IP of the database server. + +### `LD_DB_PORT` + +Values: `Integer` | Default = None + +The port of the database server. +Should use the default port if left empty, for example `5432` for PostgresSQL. diff --git a/requirements.prod.txt b/requirements.prod.txt index 6051a8c..a26f19b 100644 --- a/requirements.prod.txt +++ b/requirements.prod.txt @@ -12,6 +12,7 @@ django-widget-tweaks==1.4.12 django4-background-tasks==1.2.7 djangorestframework==3.13.1 idna==3.3 +psycopg2==2.9.5 python-dateutil==2.8.2 pytz==2022.2.1 requests==2.28.1 diff --git a/requirements.txt b/requirements.txt index 5c7c660..490b3b7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,6 +17,7 @@ django4-background-tasks==1.2.7 djangorestframework==3.13.1 idna==3.3 libsass==0.21.0 +psycopg2-binary==2.9.5 python-dateutil==2.8.2 pytz==2022.2.1 rcssmin==1.1.0 diff --git a/siteroot/settings/base.py b/siteroot/settings/base.py index 11b59e0..e5cf817 100644 --- a/siteroot/settings/base.py +++ b/siteroot/settings/base.py @@ -79,16 +79,6 @@ DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' WSGI_APPLICATION = 'siteroot.wsgi.application' -# Database -# https://docs.djangoproject.com/en/2.2/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'data', 'db.sqlite3'), - } -} - # Password validation # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators @@ -204,3 +194,31 @@ trusted_origins = os.getenv('LD_CSRF_TRUSTED_ORIGINS', '') if trusted_origins: CSRF_TRUSTED_ORIGINS = trusted_origins.split(',') +# Database +# https://docs.djangoproject.com/en/2.2/ref/settings/#databases + +LD_DB_ENGINE = os.getenv('LD_DB_ENGINE', 'sqlite') +LD_DB_HOST = os.getenv('LD_DB_HOST', 'localhost') +LD_DB_DATABASE = os.getenv('LD_DB_DATABASE', 'linkding') +LD_DB_USER = os.getenv('LD_DB_USER', 'linkding') +LD_DB_PASSWORD = os.getenv('LD_DB_PASSWORD', None) +LD_DB_PORT = os.getenv('LD_DB_PORT', None) + +if LD_DB_ENGINE == 'postgres': + default_database = { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': LD_DB_DATABASE, + 'USER': LD_DB_USER, + 'PASSWORD': LD_DB_PASSWORD, + 'HOST': LD_DB_HOST, + 'PORT': LD_DB_PORT, + } +else: + default_database = { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'data', 'db.sqlite3'), + } + +DATABASES = { + 'default': default_database +}