Code Snippet

Just another Code Snippet site

[Oracle] Disable password expiration & unlock expired account

Retrieve expired accounts :

select username, account_status from dba_users 
where ACCOUNT_STATUS LIKE '%EXPIRED%';

Retrieve profile :

select profile from DBA_USERS where username = 'USERNAME'

Update profile :

alter profile PROFILE_NAME limit password_life_time UNLIMITED;
select resource_name,limit from dba_profiles where profile='PROFILE_NAME'

Unlock user / renew password :

alter user USERNAME identified by PASSWORD account unlock;

,


5 thoughts on “[Oracle] Disable password expiration & unlock expired account

  • Olivier says:

    Linked error :
    ORA-28001: the password has expired

  • Olivier says:

    Unlock users :

    DECLARE
    
    USER_PASSWORD VARCHAR2(50) := 'password';
    NB_UNLOCKED PLS_INTEGER := 0;
    
    CURSOR C_LOCKED_USERS IS
        select username, account_status, 'alter user '||username||' identified by '||USER_PASSWORD||' account unlock' as sqlStmt
        from dba_users 
        where ACCOUNT_STATUS LIKE '%EXPIRED%'
        and (
            username like 'SCOTT%'
        );
    
    BEGIN
    
        FOR lockedUser IN C_LOCKED_USERS LOOP
        
            dbms_output.put_line('Unlocking user '||lockedUser.username );
            EXECUTE IMMEDIATE lockedUser.sqlStmt;
            NB_UNLOCKED := NB_UNLOCKED+1;
        
        END LOOP;
        
        dbms_output.put_line('Nb user(s) unlocked : '||NB_UNLOCKED );
    
    END;
    
  • Olivier says:
    select 'alter user '||username||' identified by password account unlock; ' 
    from dba_users where ACCOUNT_STATUS LIKE '%EXPIRED%' order by username;
    
  • Lock an account :

    alter user username account lock;
    
  • Unlock all user (inside try/catch) and update default profile:

    DECLARE
     
    USER_PASSWORD VARCHAR2(50) := 'password';
    NB_UNLOCKED PLS_INTEGER := 0;
     
    CURSOR C_LOCKED_USERS IS
        select username, account_status, 'alter user '||username||' identified by '||USER_PASSWORD||' account unlock' as sqlStmt
        from dba_users 
        where ACCOUNT_STATUS LIKE '%EXPIRED%';
     
    BEGIN
    
        dbms_output.put_line('Update DEFAULT profile');
        EXECUTE IMMEDIATE 'alter profile DEFAULT limit password_life_time UNLIMITED';
     
        FOR lockedUser IN C_LOCKED_USERS LOOP
         
            BEGIN
                dbms_output.put_line('Unlock user '||lockedUser.username );
                EXECUTE IMMEDIATE lockedUser.sqlStmt;
                NB_UNLOCKED := NB_UNLOCKED+1;
            EXCEPTION 
                WHEN OTHERS 
                THEN dbms_output.put_line(lockedUser.username || ' - ERROR: ' ||SQLERRM(SQLCODE));
            END;
    
         
        END LOOP;
         
        dbms_output.put_line('Nb user(s) unlocked : '||NB_UNLOCKED );
     
    END;
    

Leave a Reply to Olivier Cancel reply

Your email address will not be published. Required fields are marked *