Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ sendTo('sql.0', 'getEnabledDPs', {}, function (result) {

## Changelog
### **WORK IN PROGRESS**
* (@DutchmanNL) PostgreSQL: "do not create database" now connects straight to the configured database instead of opening the maintenance database `postgres` first, so roles without `CONNECT` on it can be used
* (@GermanBluefox) MySQL can now connect through a unix socket instead of host and port (#104)
* (@DutchmanNL) Fixed `info.connection` staying `true` while the database was unreachable
* (@GermanBluefox) Fixed crash (UNCAUGHT_EXCEPTION) when the database server closes the connection: the driver's `error` event had no listener (#527)
Expand Down
71 changes: 35 additions & 36 deletions build/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/main.js.map

Large diffs are not rendered by default.

74 changes: 38 additions & 36 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,9 +829,17 @@ export class SqlAdapter extends Adapter {
sqLiteOptions = { fileName: this.getSqlLiteDir(this.config.fileName) };
}

if (this.config.dbtype === 'postgresql' && !this.postgresDbCreated && postgreSQLOptions) {
if (
this.config.dbtype === 'postgresql' &&
!this.config.doNotCreateDatabase &&
!this.postgresDbCreated &&
postgreSQLOptions
) {
// special solution for postgres. Connect first to Db "postgres", create new DB "iobroker" and then connect to "iobroker" DB.
// connect first to DB postgres and create iobroker DB
// connect first to DB postgres and create iobroker DB.
// With "do not create database" this whole phase is skipped and the adapter connects
// straight to the configured database: a restricted role or a managed PostgreSQL often
// has no access to the maintenance DB "postgres" at all (#404, #285).
this.log.info(
`Postgres connection options: ${JSON.stringify(postgreSQLOptions).replace((postgreSQLOptions.password as string) || '******', '****')}`,
);
Expand All @@ -851,40 +859,30 @@ export class SqlAdapter extends Adapter {
return;
}

if (this.config.doNotCreateDatabase) {
_client.execute(`CREATE DATABASE ${this.config.dbname};`, (err: Error | null): void => {
_client.disconnect();
this.postgresDbCreated = true;
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = null;
this.connect(callback);
}, 100);
} else {
_client.execute(`CREATE DATABASE ${this.config.dbname};`, (err: Error | null): void => {
_client.disconnect();
const typedError: {
code: string;
} = err as any;
if (typedError && typedError.code !== '42P04') {
// if error not about yet exists
this.postgresDbCreated = false;
this.logConnectionError(typedError, `Cannot create database ${this.config.dbname}`);
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = null;
this.connect(callback);
}, 30000);
} else {
// remember that DB is created
this.postgresDbCreated = true;
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = null;
this.connect(callback);
}, 100);
}
});
}
const typedError: {
code: string;
} = err as any;
if (typedError && typedError.code !== '42P04') {
// if error not about yet exists
this.postgresDbCreated = false;
this.logConnectionError(typedError, `Cannot create database ${this.config.dbname}`);
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = null;
this.connect(callback);
}, 30000);
} else {
// remember that DB is created
this.postgresDbCreated = true;
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = null;
this.connect(callback);
}, 100);
}
});
});
}

Expand Down Expand Up @@ -1005,7 +1003,11 @@ export class SqlAdapter extends Adapter {
user: config.user || '',
password: config.password || '',
port: config.port || undefined,
database: 'postgres',
// With "do not create database" the configured database must be tested directly: a
// restricted role or a managed PostgreSQL may have no access to the maintenance DB
// "postgres" at all, so testing against it reports a failure for a perfectly good
// configuration (#404, #285).
database: config.doNotCreateDatabase ? config.dbname || 'iobroker' : 'postgres',
ssl: config.encrypt
? {
rejectUnauthorized: !!config.rejectUnauthorized,
Expand Down
Loading
Loading