ORA-01045 User Lacks CREATE SESSION Privilege - Access Permission Fix
ORA-01045: User Lacks CREATE SESSION Privilege
Section titled “ORA-01045: User Lacks CREATE SESSION Privilege”Error Overview
Section titled “Error Overview”Error Text: ORA-01045: user [username] lacks CREATE SESSION privilege; logon denied
This error occurs when a database user attempts to connect to Oracle but doesn’t have the necessary CREATE SESSION system privilege. It’s a security-related error that prevents unauthorized access to the database.
Understanding Oracle Privileges
Section titled “Understanding Oracle Privileges”System Privileges vs Object Privileges
Section titled “System Privileges vs Object Privileges”Oracle Privileges├── System Privileges (database-wide)│ ├── CREATE SESSION - Connect to database│ ├── CREATE TABLE - Create tables│ ├── CREATE VIEW - Create views│ └── DBA - Full database admin└── Object Privileges (specific objects) ├── SELECT on table ├── INSERT on table └── EXECUTE on procedure
CREATE SESSION Privilege
Section titled “CREATE SESSION Privilege”- Purpose: Allows user to connect to the database
- Required for: Any database connection
- Granted by: DBA or user with GRANT ANY PRIVILEGE
Common Causes
Section titled “Common Causes”New User Without Privileges
Section titled “New User Without Privileges”-- User created but no privileges grantedCREATE USER new_user IDENTIFIED BY password;-- User cannot connect without CREATE SESSION
Revoked Privileges
Section titled “Revoked Privileges”-- Privilege was revokedREVOKE CREATE SESSION FROM username;-- User can no longer connect
Role-Based Access Issues
Section titled “Role-Based Access Issues”-- User has role but role lacks CREATE SESSION-- Or role is not granted properly
Diagnostic Queries
Section titled “Diagnostic Queries”Check User Privileges
Section titled “Check User Privileges”-- Check direct system privileges for userSELECT * FROM dba_sys_privsWHERE grantee = 'USERNAME';
-- Check role-based privilegesSELECT * FROM dba_role_privsWHERE grantee = 'USERNAME';
-- Check privileges through rolesSELECT rp.role, sp.privilegeFROM dba_role_privs rp, dba_sys_privs spWHERE rp.grantee = 'USERNAME'AND rp.role = sp.granteeAND sp.privilege = 'CREATE SESSION';
Verify User Status
Section titled “Verify User Status”-- Check if user exists and account statusSELECT username, account_status, lock_date, expiry_dateFROM dba_usersWHERE username = 'USERNAME';
Check Current Session Privileges
Section titled “Check Current Session Privileges”-- Check privileges for current sessionSELECT * FROM session_privsWHERE privilege = 'CREATE SESSION';
-- Check effective rolesSELECT * FROM session_roles;
Resolution Steps
Section titled “Resolution Steps”Grant CREATE SESSION Directly
Section titled “Grant CREATE SESSION Directly”-- Connect as DBA or privileged userGRANT CREATE SESSION TO username;
-- Verify the grantSELECT * FROM dba_sys_privsWHERE grantee = 'USERNAME'AND privilege = 'CREATE SESSION';
Grant Through Role
Section titled “Grant Through Role”-- Grant CONNECT role (includes CREATE SESSION)GRANT CONNECT TO username;
-- Or create custom roleCREATE ROLE app_user_role;GRANT CREATE SESSION TO app_user_role;GRANT app_user_role TO username;
Multiple User Grant
Section titled “Multiple User Grant”-- Grant to multiple users at onceGRANT CREATE SESSION TO user1, user2, user3;
-- Grant role to multiple usersGRANT CONNECT TO user1, user2, user3;
Common Resolution Patterns
Section titled “Common Resolution Patterns”New Application User Setup
Section titled “New Application User Setup”-- Complete new user setupCREATE USER app_user IDENTIFIED BY secure_passwordDEFAULT TABLESPACE usersTEMPORARY TABLESPACE tempQUOTA UNLIMITED ON users;
-- Grant basic connection privilegesGRANT CONNECT TO app_user;GRANT RESOURCE TO app_user;
-- Additional privileges as neededGRANT CREATE VIEW TO app_user;GRANT CREATE PROCEDURE TO app_user;
Service Account Setup
Section titled “Service Account Setup”-- Service account with minimal privilegesCREATE USER service_account IDENTIFIED BY passwordACCOUNT UNLOCK;
-- Grant only necessary privilegesGRANT CREATE SESSION TO service_account;GRANT SELECT ON specific_table TO service_account;
-- Optional: Create dedicated roleCREATE ROLE service_role;GRANT CREATE SESSION TO service_role;GRANT SELECT ON app_table TO service_role;GRANT service_role TO service_account;
Bulk User Management
Section titled “Bulk User Management”-- Script for multiple usersDECLARE TYPE user_array IS TABLE OF VARCHAR2(30); users user_array := user_array('USER1', 'USER2', 'USER3');BEGIN FOR i IN users.FIRST..users.LAST LOOP EXECUTE IMMEDIATE 'GRANT CREATE SESSION TO ' || users(i); DBMS_OUTPUT.PUT_LINE('Granted CREATE SESSION to ' || users(i)); END LOOP;END;/
Advanced Scenarios
Section titled “Advanced Scenarios”Profile-Based Access Control
Section titled “Profile-Based Access Control”-- Check if user profile restricts sessionsSELECT username, profile FROM dba_users WHERE username = 'USERNAME';
-- Check profile limitsSELECT * FROM dba_profilesWHERE profile = (SELECT profile FROM dba_users WHERE username = 'USERNAME')AND resource_name IN ('SESSIONS_PER_USER', 'IDLE_TIME', 'CONNECT_TIME');
-- Modify profile if neededALTER PROFILE user_profile LIMIT SESSIONS_PER_USER 5;
Conditional Privilege Granting
Section titled “Conditional Privilege Granting”-- Grant privilege only if user existsDECLARE user_count NUMBER;BEGIN SELECT COUNT(*) INTO user_count FROM dba_users WHERE username = 'TARGET_USER';
IF user_count > 0 THEN EXECUTE IMMEDIATE 'GRANT CREATE SESSION TO TARGET_USER'; DBMS_OUTPUT.PUT_LINE('CREATE SESSION granted to TARGET_USER'); ELSE DBMS_OUTPUT.PUT_LINE('User TARGET_USER does not exist'); END IF;END;/
Role Hierarchy Management
Section titled “Role Hierarchy Management”-- Create role hierarchyCREATE ROLE basic_user_role;CREATE ROLE power_user_role;CREATE ROLE admin_user_role;
-- Grant basic privilegesGRANT CREATE SESSION TO basic_user_role;
-- Build hierarchyGRANT basic_user_role TO power_user_role;GRANT CREATE TABLE, CREATE VIEW TO power_user_role;
GRANT power_user_role TO admin_user_role;GRANT CREATE USER TO admin_user_role;
-- Assign to usersGRANT basic_user_role TO regular_user;GRANT power_user_role TO developer_user;GRANT admin_user_role TO dba_user;
Troubleshooting Steps
Section titled “Troubleshooting Steps”Verify Connection Attempt
Section titled “Verify Connection Attempt”-- Test connection as the userCONNECT username/password@database
-- If successful, check session infoSELECT user, sys_context('userenv', 'session_user') FROM dual;
Check for Account Issues
Section titled “Check for Account Issues”-- Check for locked accountsSELECT username, account_status, lock_dateFROM dba_usersWHERE username = 'USERNAME'AND account_status != 'OPEN';
-- Unlock if neededALTER USER username ACCOUNT UNLOCK;
Audit Connection Attempts
Section titled “Audit Connection Attempts”-- Enable login auditingAUDIT CREATE SESSION;
-- Check audit trailSELECT username, timestamp, action_name, returncodeFROM dba_audit_trailWHERE username = 'USERNAME'AND action_name = 'LOGON'ORDER BY timestamp DESC;
Monitoring and Management
Section titled “Monitoring and Management”Privilege Monitoring Script
Section titled “Privilege Monitoring Script”-- Monitor users without CREATE SESSIONSELECT u.username, u.account_statusFROM dba_users uWHERE u.username NOT IN ( -- Users with direct CREATE SESSION SELECT grantee FROM dba_sys_privs WHERE privilege = 'CREATE SESSION' UNION -- Users with CREATE SESSION through roles SELECT rp.grantee FROM dba_role_privs rp, dba_sys_privs sp WHERE rp.role = sp.grantee AND sp.privilege = 'CREATE SESSION')AND u.username NOT IN ('SYS', 'SYSTEM', 'ANONYMOUS')ORDER BY u.username;
Automated Privilege Assignment
Section titled “Automated Privilege Assignment”-- Procedure to grant standard privileges to new usersCREATE OR REPLACE PROCEDURE grant_standard_access(p_username VARCHAR2) ASBEGIN -- Check if user exists DECLARE v_count NUMBER; BEGIN SELECT COUNT(*) INTO v_count FROM dba_users WHERE username = UPPER(p_username);
IF v_count = 0 THEN RAISE_APPLICATION_ERROR(-20001, 'User does not exist: ' || p_username); END IF; END;
-- Grant standard privileges EXECUTE IMMEDIATE 'GRANT CONNECT TO ' || p_username; EXECUTE IMMEDIATE 'GRANT RESOURCE TO ' || p_username;
DBMS_OUTPUT.PUT_LINE('Standard access granted to ' || p_username);END;/
Access Review Report
Section titled “Access Review Report”-- Generate access review reportSELECT u.username, u.account_status, u.lock_date, u.expiry_date, CASE WHEN EXISTS ( SELECT 1 FROM dba_sys_privs WHERE grantee = u.username AND privilege = 'CREATE SESSION' ) THEN 'DIRECT' WHEN EXISTS ( SELECT 1 FROM dba_role_privs rp, dba_sys_privs sp WHERE rp.grantee = u.username AND rp.role = sp.grantee AND sp.privilege = 'CREATE SESSION' ) THEN 'ROLE-BASED' ELSE 'NO ACCESS' END as session_accessFROM dba_users uWHERE u.username NOT LIKE '%$%' -- Exclude system accountsORDER BY u.username;
Security Best Practices
Section titled “Security Best Practices”Principle of Least Privilege
Section titled “Principle of Least Privilege”-- Create role-based accessCREATE ROLE app_readonly_role;GRANT CREATE SESSION TO app_readonly_role;GRANT SELECT ON app_table TO app_readonly_role;
CREATE ROLE app_readwrite_role;GRANT app_readonly_role TO app_readwrite_role;GRANT INSERT, UPDATE, DELETE ON app_table TO app_readwrite_role;
-- Grant appropriate role to usersGRANT app_readonly_role TO report_user;GRANT app_readwrite_role TO app_user;
Password and Account Management
Section titled “Password and Account Management”-- Create user with strong password policyCREATE USER secure_user IDENTIFIED BY complex_passwordPASSWORD EXPIREACCOUNT UNLOCK;
-- Grant minimal required privilegesGRANT CREATE SESSION TO secure_user;
-- Set appropriate profileALTER USER secure_user PROFILE restricted_profile;
Emergency Procedures
Section titled “Emergency Procedures”Grant Emergency Access
Section titled “Grant Emergency Access”-- Emergency access script (run as DBA)-- Grant CREATE SESSION to locked-out userALTER USER emergency_user ACCOUNT UNLOCK;GRANT CREATE SESSION TO emergency_user;
-- Temporary elevated access if neededGRANT DBA TO emergency_user; -- CAUTION: Very powerful
-- Remember to revoke after emergency-- REVOKE DBA FROM emergency_user;
Bulk Access Restore
Section titled “Bulk Access Restore”-- Restore access for multiple usersBEGIN FOR rec IN ( SELECT username FROM dba_users WHERE account_status = 'LOCKED' AND username LIKE 'APP_%' ) LOOP EXECUTE IMMEDIATE 'ALTER USER ' || rec.username || ' ACCOUNT UNLOCK'; EXECUTE IMMEDIATE 'GRANT CREATE SESSION TO ' || rec.username; DBMS_OUTPUT.PUT_LINE('Access restored for ' || rec.username); END LOOP;END;/
Related Errors
Section titled “Related Errors”- ORA-00988: Missing or invalid password
- ORA-28000: Account is locked
- ORA-28001: Password has expired
- ORA-01017: Invalid username/password
Best Practices
Section titled “Best Practices”- Use role-based access control
- Follow principle of least privilege
- Regular access reviews and audits
- Document privilege assignments
- Monitor failed connection attempts
- Use strong password policies
Troubleshooting Checklist
Section titled “Troubleshooting Checklist”- Verify user exists in database
- Check account status (locked/expired)
- Confirm CREATE SESSION privilege
- Review role assignments
- Check profile restrictions
- Test connection with correct credentials
- Review audit logs for connection attempts
- Verify network connectivity