简体   繁体   中英

Regarding Users in Oracle 12c

I have installed an Oracle 12c database on my system. I had an application which need to access the database.

Previously in Oracle 11g, I used the following commands to create an user.

create user name identified by name;  
grant connect,create session,resource,create view to name;

Can anyone tell me how to create a user in Oracle 12c with my above requirements? I used the following statements but my installation is showing a fatal error saying

FATAL ERROR - java.sql.SQLException: ORA-01950: no privileges on tablespace 'USERS'

Following were the statements used.

create user c##test1 identified by test1 container = ALL;
grant connect,create session,resource,create view to test1;

Best Practice is to create a tablespace and assign that to the User.

Just to make it easier to understand use same name for username and tablespace

CREATE BIGFILE TABLESPACE C##1
DATAFILE '/path/to/datafile/C##1.dbf'
SIZE 10M AUTOEXTEND ON NEXT 5M MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO NOLOGGING;


--Create User

CREATE USER C##1
IDENTIFIED BY password DEFAULT TABLESPACE C##1
QUOTA UNLIMITED ON C##1;

You should also give the user a quota on his default tablespace:

CREATE USER name
IDENTIFIED BY name
DEFAULT TABLESPACE users
TEMPORARY TABLESPACE temp
QUOTA 50M /* or any other number that makes sense */ ON users

GRANT CONNECT, CREATE SESSION, RESOURCE, CREATE VIEW TO name;

Try this:

#!/bin/bash
USERVALID=false;

if [ -z $1 ]; then
 echo -e "\nThis script will create a new common user in Oracle"
fi

while [[ $USERVALID == false ]]; do
 if [ -z $1 ]; then
  echo -e "Username must start with 'c##' or 'C##'\n"
  read -p "Enter a username: " NEWUSER
 else
  NEWUSER=$1
 fi
 if [[ $NEWUSER == c##* ]]
 then
  USERVALID=true;
 elif [[ $NEWUSER == C##* ]]
 then
  USERVALID=true;
 else
  USERVALID=false;
  echo -e "\nInvalid username";
  if [ ! -z $1 ]; then
   echo -e "Username must start with 'c##' or 'C##'\n"
   exit 0
  fi
 fi
done

while [[ -z $NEWPWD ]]; do
 if [ -z $2 ]; then
  read -p "Enter a password: " NEWPWD
 else
  NEWPWD=$2
 fi
 if [[ -z $NEWPWD ]]
 then
  echo -e "Password must not be NULL"
 fi
done

get_user_status () {
 sqlplus -s / as sysdba <<!
 set heading off
 set feedback off
 set pages 0
 select username from all_users where username = '${NEWUSER^^}';
!
}

USERDROP=$(get_user_status)
if [[ $USERDROP =~ ${NEWUSER^^} ]]; then
 echo -e "\nUser already exists...\nAttempting to drop user\n"
 echo -e "DROP USER $NEWUSER CASCADE;" | sqlplus -s / as sysdba
 USERDROP=$(get_user_status)
 if [[ $USERDROP =~ ${NEWUSER^^} ]]; then
  echo -e "Please ensure user is disconnected from the database before proceeding.\n"
  exit 0
 fi
fi

echo -e "CREATE USER $NEWUSER IDENTIFIED BY $NEWPWD;"  | sqlplus -s / as sysdba

USERDROP=$(get_user_status)
if [[ ! $USERDROP =~ ${NEWUSER^^} ]]; then
 echo -e "You are a bad person.\n"
 exit 0
fi

echo -e "GRANT CREATE SESSION TO $NEWUSER;"            | sqlplus -s / as sysdba
echo -e "GRANT CREATE PROCEDURE TO $NEWUSER;"          | sqlplus -s / as sysdba
echo -e "GRANT CREATE SEQUENCE TO $NEWUSER;"           | sqlplus -s / as sysdba
echo -e "GRANT CREATE DATABASE LINK TO $NEWUSER;"      | sqlplus -s / as sysdba
echo -e "GRANT CREATE TABLE TO $NEWUSER;"              | sqlplus -s / as sysdba
echo -e "GRANT CREATE VIEW TO $NEWUSER;"               | sqlplus -s / as sysdba
echo -e "GRANT CREATE MATERIALIZED VIEW TO $NEWUSER;"  | sqlplus -s / as sysdba
echo -e "GRANT QUERY REWRITE TO $NEWUSER;"             | sqlplus -s / as sysdba
echo -e "GRANT SELECT ANY TABLE TO $NEWUSER;"          | sqlplus -s / as sysdba
echo -e "GRANT SELECT ON SYS.V_\$SESSION TO $NEWUSER;" | sqlplus -s / as sysdba
echo -e "GRANT EXECUTE ON SYS.DBMS_LOCK TO $NEWUSER;"  | sqlplus -s / as sysdba
echo -e "GRANT UNLIMITED TABLESPACE TO $NEWUSER;"      | sqlplus -s / as sysdba

echo -e "\nAll done!\n"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM