add createdatabase: false option to postgres for users with no permissions #7577
Signed-off-by: si458 <simonsmith5521@gmail.com>
si458 committed
Jan 29, 2026 at 13:44 UTC
b1910a197830bffb02abd4e523dab033600a4345
2 files changed
+27
-6
db.js
+22
-6
@@ -922,18 +922,33 @@ module.exports.CreateDB = function (parent, func) {
922
}
923
} else if (parent.args.postgres) {
924
// Postgres SQL
925
- let connectinArgs = parent.args.postgres;
926
- connectinArgs.database = (databaseName = (connectinArgs.database != null) ? connectinArgs.database : 'meshcentral');
925
+ let connectionArgs = parent.args.postgres;
926
+ connectionArgs.database = (databaseName = (connectionArgs.database != null) ? connectionArgs.database : 'meshcentral');
927
928
let DatastoreTest;
929
obj.databaseType = DB_POSTGRESQL;
930
const { Client } = require('pg');
931
- Datastore = new Client(connectinArgs);
931
+ Datastore = new Client(connectionArgs);
932
+ // Check if we should skip database creation check
933
+ if (connectionArgs.createdatabase === false ) {
934
+ // Skip database check/creation, just connect and run the SELECT query
935
+ Datastore.connect();
936
+ Datastore.query('SELECT doc FROM main WHERE id = $1', ['DatabaseIdentifier'], function (err, res) {
937
+ if (err == null) {
938
+ (res.rowCount == 0) ? postgreSqlCreateTables(func) : setupFunctions(func);
939
+ } else if (err.code == '42P01') { //42P01 = undefined table
940
+ postgreSqlCreateTables(func);
941
+ } else {
942
+ console.log('Postgresql connection error: ', err.message);
943
+ process.exit(0);
944
+ }
945
+ });
946
+ } else {
947
//Connect to and check pg db first to check if own db exists. Otherwise errors out on 'database does not exist'
933
- connectinArgs.database = 'postgres';
934
- DatastoreTest = new Client(connectinArgs);
948
+ connectionArgs.database = 'postgres';
949
+ DatastoreTest = new Client(connectionArgs);
950
DatastoreTest.connect();
936
- connectinArgs.database = databaseName; //put the name back for backupconfig info
951
+ connectionArgs.database = databaseName; //put the name back for backupconfig info
952
DatastoreTest.query('SELECT 1 FROM pg_catalog.pg_database WHERE datname = $1', [databaseName], function (err, res) { // check database exists first before creating
953
if (res.rowCount != 0) { // database exists now check tables exists
954
DatastoreTest.end();
@@ -963,6 +978,7 @@ module.exports.CreateDB = function (parent, func) {
978
});
979
}
980
});
981
+ }
982
} else if (parent.args.mongodb) {
983
// Use MongoDB
984
obj.databaseType = DB_MONGODB;
meshcentral-config-schema.json
+5
@@ -210,6 +210,11 @@
210
"type": "string",
211
"default": "meshcentral",
212
"description": "Name of PostgreSQL database used"
213
+ },
214
+ "createdatabase": {
215
+ "type": "boolean",
216
+ "default": true,
217
+ "description": "Set to false to skip database existence check and creation. Useful when the connected user does not have permission to create databases."
218
}
219
}
220
},