Learning from Expertise #3: Why cannot remove AD Admin or set a new one on Azure PostgreSQL Server?!

Published Jul 05 2021 04:02 AM 2,431 Views
Microsoft

Overview:

We see customers were not able to delete the AD Admin user in Azure database for PostgreSQL, and after triggering the operation , the request keep running forever without any error message or it fails with error like:

'The user name conflict with an existing database user name' is not a valid name because it contains invalid characters.

 

Solution:

In this scenario, most properly the operation stuck because there is a dependent objects on the AAD role, so you will need to remove these dependencies first , so log in with your server admin user and check the object dependency using the following query:

Ahmed_S_Mahmoud_0-1625481541648.png

Note:- 

-- make sure to run this query against all databases.

-- Replace ####### with AAD admin User or group.

 

WITH myconstants (MyUser, ExcludeUser) AS (
    values ('########', 'azure_superuser')
)
SELECT
    n.nspname AS schema_name,
    c.relname AS rel_name,
    c.relkind AS rel_kind,
    pg_get_userbyid(c.relowner) AS owner_name
  FROM myconstants, pg_class c
  JOIN pg_namespace n ON n.oid = c.relnamespace
  WHERE
        pg_get_userbyid(c.relowner) NOT LIKE concat('%',ExcludeUser,'%')
    AND
        pg_get_userbyid(c.relowner) LIKE concat('%',MyUser,'%')
UNION ALL
SELECT
    n.nspname AS schema_name,
    p.proname,
    'p',
    pg_get_userbyid(p.proowner)
 FROM myconstants, pg_proc p
  JOIN pg_namespace n ON n.oid = p.pronamespace
  WHERE
        pg_get_userbyid(p.proowner) NOT LIKE concat ('%',ExcludeUser,'%')
    AND
        pg_get_userbyid(p.proowner) LIKE concat('%',MyUser,'%')

 

To mitigate this issue, you will need to change the ownership of these object to other new/existing user and retry the operation:

 

REASSIGN OWNED BY "#######" TO "local database user";
DROP OWNED BY "#######";

 

 

 

For more details, please check out PostgreSQL documentation:

PostgreSQL: Documentation: 11: REASSIGN OWNED

In case, after you cleaned the dependences to AAD Admin and you want to set the AD Admin again but it fails again with error

 

 

 

'The user name conflict with an existing database user name' is not a valid name because it contains invalid characters.

 

As a workaround, you can assign the AAD admin role to another AAD admin user and then you will be able to do the override. by then you should be able to reset the AAD admin once again.

 

I hope you find this article helpful. If you have any feedback please do not hesitate to provide it in the comment section below.

 

Ahmed S. Mazrouh

%3CLINGO-SUB%20id%3D%22lingo-sub-2515655%22%20slang%3D%22en-US%22%3ELearning%20from%20Expertise%20%233%3A%20Why%20cannot%20remove%20AD%20Admin%20or%20set%20a%20new%20one%20on%20Azure%20PostgreSQL%20Server%3F!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2515655%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EOverview%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EWe%20see%20customers%20were%20not%20able%20to%20delete%20the%20AD%20Admin%20user%20in%20Azure%20database%20for%20PostgreSQL%2C%20and%20after%20triggering%20the%20operation%20%2C%20the%20request%20keep%20running%20forever%20without%20any%20error%20message%20or%20it%20fails%20with%20error%20like%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22ng-binding%22%3E%3CSTRONG%3E'The%20user%20name%20conflict%20with%20an%20existing%20database%20user%20name'%20is%20not%20a%20valid%20name%20because%20it%20contains%20invalid%20characters.%0A%3C%2FSTRONG%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESolution%3C%2FSTRONG%3E%3A%3C%2FP%3E%0A%3CP%3EIn%20this%20scenario%2C%20most%20properly%20the%20operation%20stuck%20because%20there%20is%20a%20dependent%20objects%20on%20the%20AAD%20role%2C%20so%20you%20will%20need%20to%20remove%20these%20dependencies%20first%20%2C%20so%20log%20in%20with%20your%20server%20admin%20user%20and%20check%20the%20object%20dependency%20using%20the%20following%20query%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Ahmed_S_Mahmoud_0-1625481541648.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293484i00A8C4FEEF83E535%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Ahmed_S_Mahmoud_0-1625481541648.png%22%20alt%3D%22Ahmed_S_Mahmoud_0-1625481541648.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3ENote%3A-%26nbsp%3B%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E--%20make%20sure%20to%20run%20this%20query%20against%20all%20databases.%3C%2FP%3E%0A%3CP%3E--%20Replace%20%23%23%23%23%23%23%23%20with%26nbsp%3BAAD%20admin%20User%20or%20group.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EWITH%20myconstants%20(MyUser%2C%20ExcludeUser)%20AS%20(%0A%20%20%20%20values%20('%23%23%23%23%23%23%23%23'%2C%20'azure_superuser')%0A)%0ASELECT%0A%20%20%20%20n.nspname%20AS%20schema_name%2C%0A%20%20%20%20c.relname%20AS%20rel_name%2C%0A%20%20%20%20c.relkind%20AS%20rel_kind%2C%0A%20%20%20%20pg_get_userbyid(c.relowner)%20AS%20owner_name%0A%20%20FROM%20myconstants%2C%20pg_class%20c%0A%20%20JOIN%20pg_namespace%20n%20ON%20n.oid%20%3D%20c.relnamespace%0A%20%20WHERE%0A%20%20%20%20%20%20%20%20pg_get_userbyid(c.relowner)%20NOT%20LIKE%20concat('%25'%2CExcludeUser%2C'%25')%0A%20%20%20%20AND%0A%20%20%20%20%20%20%20%20pg_get_userbyid(c.relowner)%20LIKE%20concat('%25'%2CMyUser%2C'%25')%0AUNION%20ALL%0ASELECT%0A%20%20%20%20n.nspname%20AS%20schema_name%2C%0A%20%20%20%20p.proname%2C%0A%20%20%20%20'p'%2C%0A%20%20%20%20pg_get_userbyid(p.proowner)%0A%20FROM%20myconstants%2C%20pg_proc%20p%0A%20%20JOIN%20pg_namespace%20n%20ON%20n.oid%20%3D%20p.pronamespace%0A%20%20WHERE%0A%20%20%20%20%20%20%20%20pg_get_userbyid(p.proowner)%20NOT%20LIKE%20concat%20('%25'%2CExcludeUser%2C'%25')%0A%20%20%20%20AND%0A%20%20%20%20%20%20%20%20pg_get_userbyid(p.proowner)%20LIKE%20concat('%25'%2CMyUser%2C'%25')%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ETo%20mitigate%20this%20issue%2C%20you%20will%20need%20to%20change%20the%20ownership%20of%20these%20object%20to%20other%20new%2Fexisting%20user%20and%20retry%20the%20operation%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EREASSIGN%20OWNED%20BY%20%22%23%23%23%23%23%23%23%22%20TO%20%22local%20database%20user%22%3B%0ADROP%20OWNED%20BY%20%22%23%23%23%23%23%23%23%22%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20more%20details%2C%20please%20check%20out%20PostgreSQL%20documentation%3A%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F11%2Fsql-reassign-owned.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EPostgreSQL%3A%20Documentation%3A%2011%3A%20REASSIGN%20OWNED%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EIn%20case%2C%20after%20you%20cleaned%20the%20dependences%20to%20AAD%20Admin%20and%20you%20want%20to%20set%20the%20AD%20Admin%20again%20but%20it%20fails%20again%20with%20error%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-css%22%3E%3CCODE%3E'The%20user%20name%20conflict%20with%20an%20existing%20database%20user%20name'%20is%20not%20a%20valid%20name%20because%20it%20contains%20invalid%20characters.%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20a%20workaround%2C%20you%20can%20assign%20the%20AAD%20admin%20role%20to%20another%20AAD%20admin%20user%20and%20then%20you%20will%20be%20able%20to%20do%20the%20override.%20by%20then%20you%20should%20be%20able%20to%20reset%20the%20AAD%20admin%20once%20again.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20hope%20you%20find%20this%20article%20helpful.%20If%20you%20have%20any%20feedback%20please%20do%20not%20hesitate%20to%20provide%20it%20in%20the%20comment%20section%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAhmed%20S.%20Mazrouh%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Jul 05 2021 04:05 AM
Updated by: