Repository: mathesar-foundation/mathesar

Issue:
The requirement of superuser postgresql access is problematic
## Problem
Mathesar needs a Postgres superuser to function correctly, from the docs at https://docs.mathesar.org/installation/build-from-source/ 

## Proposed solution
The mathesar user should not require superuser access. 

## Additional context
The superuser is a global permission meaning that a user that has superuser permission will be able to access (and do stuff) not on the mathesar but *all* the databases of the RDBMS. Considering that many production systems have a single RDBMS hosting many application this is a major problem since the mathsar user won't have any access boundaries on the same RDBMS. The mathesar user access can be unlimited but *must* be bounded without the mathesar database.


Discussion:
Thanks for reporting this, @spapas. This is already a high-priority issue for us to resolve, requiring a superuser is one of the compromises we made to get our alpha version out of the door.
Hello friends, any news on this issue? This is very important for us and we can't actually use mathesar in procution until the supertuser requirement is dropped. 

Would it be possible to clarify a bit why the mathesar usr needs global superuser access ? What would happen if we removed the RDBMS superuser access on the mathesar user and made him an owner of the corresponding databases (i.e mathesar_data and mathesar_django) ? 

Thank you
Hi @spapas 

We are working on this issue under the [Installations Improvement Project](https://wiki.mathesar.org/en/projects/installation-improvements-0_1_3). This will be part of Release v0.1.3 (coming next month)
Thank you @rajatvijay for answering me & giving a delivery estimate!

Kind regards,
Serafeim

Resolving patch:
diff --git a/db/install.py b/db/install.py
--- a/db/install.py
+++ b/db/install.py
@@ -1,5 +1,6 @@
+from psycopg.errors import InsufficientPrivilege
 from sqlalchemy import text
-from sqlalchemy.exc import OperationalError
+from sqlalchemy.exc import OperationalError, ProgrammingError
 
 from db import engine
 from db.sql import install as sql_install
@@ -54,12 +55,20 @@ def _create_database(database_name, hostname, username, password, port, skip_con
             username, password, hostname, root_database, port,
             connect_args={"connect_timeout": 10}
         )
-        with root_db_engine.connect() as conn:
-            conn.execution_options(isolation_level="AUTOCOMMIT")
-            conn.execute(text(f'CREATE DATABASE "{database_name}"'))
-        root_db_engine.dispose()
-        print(f"Created DB is {database_name}.")
-        return True
+        try:
+            with root_db_engine.connect() as conn:
+                conn.execution_options(isolation_level="AUTOCOMMIT")
+                conn.execute(text(f'CREATE DATABASE "{database_name}"'))
+            root_db_engine.dispose()
+            print(f"Created DB is {database_name}.")
+            return True
+        except ProgrammingError as e:
+            if isinstance(e.orig, InsufficientPrivilege):
+                print(f"Database {database_name} could not be created due to Insufficient Privilege")
+                return False
+        except Exception:
+            print(f"Database {database_name} could not be created!")
+            return False
     else:
         print(f"Database {database_name} not created!")
         return False


Test patch: