1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-03-20 20:55:18 +02:00

Server: Add in ability to use Postgres connection string in configuration (#6836)

This commit is contained in:
Gavin Mogan 2022-12-28 06:38:30 -08:00 committed by GitHub
parent 5b3f07f3c5
commit 01d451f72a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 13 deletions

View File

@ -34,7 +34,9 @@ You can setup the container to either use an existing PostgreSQL server, or conn
### Using an existing PostgreSQL server ### Using an existing PostgreSQL server
To use an existing PostgresSQL server, set the following environment variables in the .env file: To use an existing PostgresSQL server, you can variables in the .env file. Either:
#### Individual variables
```conf ```conf
DB_CLIENT=pg DB_CLIENT=pg
@ -45,6 +47,13 @@ POSTGRES_PORT=5432
POSTGRES_HOST=localhost POSTGRES_HOST=localhost
``` ```
#### Connection String
```conf
DB_CLIENT=pg
POSTGRES_CONNECTION_STRING=postgresql://username:password@your_joplin_postgres_server:5432/joplin
```
Ensure that the provided database and user exist as Joplin Server will not create them. When running on macOS or Windows through Docker Desktop, a mapping of localhost is made automatically. On Linux, you can add `--net=host --add-host=host.docker.internal:127.0.0.1` to the `docker run` command line to make the mapping happen. Any other `POSTGRES_HOST` than localhost or 127.0.0.1 should work as expected without further action. Ensure that the provided database and user exist as Joplin Server will not create them. When running on macOS or Windows through Docker Desktop, a mapping of localhost is made automatically. On Linux, you can add `--net=host --add-host=host.docker.internal:127.0.0.1` to the `docker run` command line to make the mapping happen. Any other `POSTGRES_HOST` than localhost or 127.0.0.1 should work as expected without further action.
### Using docker-compose ### Using docker-compose

View File

@ -70,7 +70,7 @@ function markPasswords(o: Record<string, any>): Record<string, any> {
const output: Record<string, any> = {}; const output: Record<string, any> = {};
for (const k of Object.keys(o)) { for (const k of Object.keys(o)) {
if (k.toLowerCase().includes('password') || k.toLowerCase().includes('secret')) { if (k.toLowerCase().includes('password') || k.toLowerCase().includes('secret') || k.toLowerCase().includes('connectionstring')) {
output[k] = '********'; output[k] = '********';
} else { } else {
output[k] = o[k]; output[k] = o[k];

View File

@ -42,15 +42,25 @@ function databaseConfigFromEnv(runningInDocker: boolean, env: EnvVariables): Dat
}; };
if (env.DB_CLIENT === 'pg') { if (env.DB_CLIENT === 'pg') {
return { const databaseConfig: DatabaseConfig = {
...baseConfig, ...baseConfig,
client: DatabaseConfigClient.PostgreSQL, client: DatabaseConfigClient.PostgreSQL,
name: env.POSTGRES_DATABASE,
user: env.POSTGRES_USER,
password: env.POSTGRES_PASSWORD,
port: env.POSTGRES_PORT,
host: databaseHostFromEnv(runningInDocker, env) || 'localhost',
}; };
if (env.POSTGRES_CONNECTION_STRING) {
return {
...databaseConfig,
connectionString: env.POSTGRES_CONNECTION_STRING,
};
} else {
return {
...databaseConfig,
name: env.POSTGRES_DATABASE,
user: env.POSTGRES_USER,
password: env.POSTGRES_PASSWORD,
port: env.POSTGRES_PORT,
host: databaseHostFromEnv(runningInDocker, env) || 'localhost',
};
}
} }
return { return {

View File

@ -45,6 +45,7 @@ export interface DbConfigConnection {
database?: string; database?: string;
filename?: string; filename?: string;
password?: string; password?: string;
connectionString?: string;
} }
export interface QueryContext { export interface QueryContext {
@ -77,11 +78,15 @@ export function makeKnexConfig(dbConfig: DatabaseConfig): KnexDatabaseConfig {
if (dbConfig.client === 'sqlite3') { if (dbConfig.client === 'sqlite3') {
connection.filename = dbConfig.name; connection.filename = dbConfig.name;
} else { } else {
connection.database = dbConfig.name; if (dbConfig.connectionString) {
connection.host = dbConfig.host; connection.connectionString = dbConfig.connectionString;
connection.port = dbConfig.port; } else {
connection.user = dbConfig.user; connection.database = dbConfig.name;
connection.password = dbConfig.password; connection.host = dbConfig.host;
connection.port = dbConfig.port;
connection.user = dbConfig.user;
connection.password = dbConfig.password;
}
} }
return { return {

View File

@ -55,6 +55,7 @@ const defaultEnvValues: EnvVariables = {
POSTGRES_USER: 'joplin', POSTGRES_USER: 'joplin',
POSTGRES_HOST: '', POSTGRES_HOST: '',
POSTGRES_PORT: 5432, POSTGRES_PORT: 5432,
POSTGRES_CONNECTION_STRING: '',
// This must be the full path to the database file // This must be the full path to the database file
SQLITE_DATABASE: '', SQLITE_DATABASE: '',
@ -124,6 +125,7 @@ export interface EnvVariables {
POSTGRES_USER: string; POSTGRES_USER: string;
POSTGRES_HOST: string; POSTGRES_HOST: string;
POSTGRES_PORT: number; POSTGRES_PORT: number;
POSTGRES_CONNECTION_STRING: string;
SQLITE_DATABASE: string; SQLITE_DATABASE: string;

View File

@ -65,6 +65,7 @@ export interface DatabaseConfig {
port?: number; port?: number;
user?: string; user?: string;
password?: string; password?: string;
connectionString?: string;
asyncStackTraces?: boolean; asyncStackTraces?: boolean;
slowQueryLogEnabled?: boolean; slowQueryLogEnabled?: boolean;
slowQueryLogMinDuration?: number; slowQueryLogMinDuration?: number;