Skip to content

Commit

Permalink
Merge branch 'develop' into feature/4928/node-copy-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
sphuber authored Sep 10, 2021
2 parents 62040f5 + fb259b7 commit 4c92cec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 5 additions & 4 deletions aiida/manage/external/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

__all__ = ('Postgres', 'PostgresConnectionMode', 'DEFAULT_DBINFO')

_CREATE_USER_COMMAND = 'CREATE USER "{}" WITH PASSWORD \'{}\''
# The last placeholder is for adding privileges of the user
_CREATE_USER_COMMAND = 'CREATE USER "{}" WITH PASSWORD \'{}\' {}'
_DROP_USER_COMMAND = 'DROP USER "{}"'
_CREATE_DB_COMMAND = (
'CREATE DATABASE "{}" OWNER "{}" ENCODING \'UTF8\' '
Expand Down Expand Up @@ -91,7 +92,7 @@ def dbuser_exists(self, dbuser):
"""
return bool(self.execute(_USER_EXISTS_COMMAND.format(dbuser)))

def create_dbuser(self, dbuser, dbpass):
def create_dbuser(self, dbuser, dbpass, privileges=''):
"""
Create a database user in postgres
Expand All @@ -100,7 +101,7 @@ def create_dbuser(self, dbuser, dbpass):
:raises: psycopg2.errors.DuplicateObject if user already exists and
self.connection_mode == PostgresConnectionMode.PSYCOPG
"""
self.execute(_CREATE_USER_COMMAND.format(dbuser, dbpass))
self.execute(_CREATE_USER_COMMAND.format(dbuser, dbpass, privileges))

def drop_dbuser(self, dbuser):
"""
Expand Down Expand Up @@ -220,7 +221,7 @@ def manual_setup_instructions(dbuser, dbname):
'Run the following commands as a UNIX user with access to PostgreSQL (Ubuntu: $ sudo su postgres):',
'',
'\t$ psql template1',
f' ==> {_CREATE_USER_COMMAND.format(dbuser, dbpass)}',
f' ==> {_CREATE_USER_COMMAND.format(dbuser, dbpass, "")}',
f' ==> {_CREATE_DB_COMMAND.format(dbname, dbuser)}',
f' ==> {_GRANT_PRIV_COMMAND.format(dbname, dbuser)}',
])
Expand Down
6 changes: 4 additions & 2 deletions aiida/manage/tests/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ def create_aiida_db(self):
if self.pg_cluster is None:
self.create_db_cluster()
self.postgres = Postgres(interactive=False, quiet=True, dbinfo=self.dbinfo)
# note: not using postgres.create_dbuser_db_safe here since we don't want prompts
self.postgres.create_dbuser(self.profile_info['database_username'], self.profile_info['database_password'])
# Note: We give the user CREATEDB privileges here, only since they are required for the migration tests
self.postgres.create_dbuser(
self.profile_info['database_username'], self.profile_info['database_password'], 'CREATEDB'
)
self.postgres.create_db(self.profile_info['database_username'], self.profile_info['database_name'])
self.dbinfo = self.postgres.dbinfo
self.profile_info['database_hostname'] = self.postgres.host_for_psycopg2
Expand Down

0 comments on commit 4c92cec

Please sign in to comment.