Which account to use when connecting to Oracle DB from script?


please can somebody help me with the following?
I'm trying to connect to Oracle from PowerShell script. I believe the only problem is in the incorrect connection string and I don't know, which user account should be used in the string.

The hostname is ORA1 and there I have only the default database called "orcl".
If I use lsnrctl status, it displays the info about runninc instance "orcl", so I think this shouldn't be a problem.
I'm able to connect to database from sqlplus. I know almost nothing about Oracle, but it seems that I can connect successfully if I use the username "sys as sysdba".
I set all the passwords to be "abc" just for simplicity.

I doesn't let me in if I use the account sysdba directly - it works only as "sys as sysdba".

Now regarding the script - it's almost the same. If I have "sys" in the connection string, I get an error message saying "you should use 'sys as sysdba' as logon name".
If I change it in the connection string and use the username "sys as sysdba", then I get an error message about incorrect password/logon denied.

Could you please tell me how to fix it? I read hundred of pages about the connection strings, tried multiple combinations, but no avail so far.

Also I'm not sure what value should be in the connection string in "data source". I have "ora1\orcl", which is the hostname\db_name. Should I use something different?

Thank you very much in advance.

LVL 13
Who is Participating?
jaiminpsoniConnect With a Mentor Commented:
what is the script?

Is it perl script? Or jython script or python script? (I am not much into it, so cant figure it from sample code)

For JDBC, we have following option...

 * user                String   The user name for logging into the
 *                                         database.
 * password            String   The password for logging into the
 *                                         database.
 * database            String   The connect string for the database.
 * internal_logon     String   A role, such as SYSDBA or SYSOPER,
 *                                         that allows you to log on as SYS.

But this is with JDBC, it will be different for string, but the property should remain same for all the DBC.

so try exploring more on internal_logon syntex for your scripting language...
sqlplus sys/abc@orcl as sysdba
if you are logged in as the oracle owner (usually a user called "oracle")

sqlplus / as sysdba
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

martin_babarikAuthor Commented:
Hi and thank you for the comment.

What you say works nice with sqlplus - I connect successfully.

But the question was more about what should be passed in the connection string. I realize I'm asking about scripting in the Oracle zone, but I believe the connection string is the same for any programming/scripting language.

I'm logged on as local administrator, it's a fresh install of Windows Server 2003 and Oracle, all default, only the password is set to 'abc'.

This problem is driving me crazy as I need to develop quite complex script and I'm stucked just first few lines where I have the connection attempt.

As long as I'm able to work with the database using sqlplus, I believe the database, services and listener are ok. Maybe it's something wrong with my script, but it's just a sample code googled on the Internet, nothing special, see below.

The table 'employees' probably doesn't exist, but didn't get so far to get an error message about this :-) It's just about the connection.

Thanks a lot.

$connectionString= "Data Source=ora1/orcl;User Id='sys/abc@orcl as sysdba';Password=abc;"
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)
$command = new-Object System.Data.OracleClient.OracleCommand($queryString, $connection)
$employeesNames = $command.ExecuteScalar()
echo "Number of employees: "$employeesNames

Open in new window

Praveen Kumar ChandrashekatrConnect With a Mentor Database Analysist Senior Commented:
why you want to login as sys any particular reason?

if not why cann't you try as system user... if you know system password.

if you don't know the system password login to database with sys through sqlplus and change the password of system.

alter user system identified by password account unlock;

then you can use system user in your script.
martin_babarikAuthor Commented:
Jaiminpsoni: The script is powershell, but I could do it using any other language I know...ehm, actually VBScript, that's all :-)
Maybe there could be the concept I don't understand - the difference between user and internal_logon.

Praveencpk: I don't need to login as sys - as I mentioned I don't know Oracle and I thought, that this is the simplest way, when I'm working in the virtual environment just for development purposes - that I will not be limited by some restrictions or insufficient privilegues.

By system user you mean a user called "system"? I altered the system user by using your command, went ok, I can login from sqlplus using this account.

Actually as I can see now, the script works! I modified the connection string to be like below and used OleDB from another googled example:
$connString = "password=abc;User ID=SYSTEM;Data Source=orcl;Provider=OraOLEDB.Oracle"

Afaik I haven't tried to login with SYSTEM user account before, maybe this was the core of the problem.

Anyway thank you very much for your help guys, I'm gonna close this question and split the points....and ask some new questions :-)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.