Thread: Not able to connect to postgresql database

Not able to connect to postgresql database

From
Manohar Bhattarai
Date:
Hi,

 I am a newbie for postgresql. I am trying to connect to postgresql database using jdbc. I googled about it and got the Driver string and connection string formats. I tried using some examples i got on the internet. But my program is not able to connect to postgresql and shows an exception that it cannot find driver. I am doing it in Ubuntu8.04 Linux with Postgresql 8.3 installed. I am using Eclipse galileo as IDE. I created database using commands in terminal. I have added the external jar files for postgresql jdbc connector(postgresql-8.3-603.jdbc3.jar, postgresql-8.3-603.jdbc4.jar, postgresql-8.4-701.jdbc3.jar and postgresql-8.4-701.jdbc4.jar) in my project also.

Please help me and let me know what i am doing wrong and what i am missing.
Can you please give an example code if possible?

Re: Not able to connect to postgresql database

From
"Kevin Grittner"
Date:
Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:

> shows an exception that it cannot find driver.

> Please help me and let me know what i am doing wrong and what i am
> missing.

The jar is not on your classpath, or you are not spelling the class
name ("org.postgresql.Driver") correctly.  I don't know of any other
reason that the recommended class loading:

    Class.forName("org.postgresql.Driver");

would throw a ClassNotFoundException.  (If that's not what you're
doing, or that's not the statement on which it fails, or that's not
your exception, you need to give us more clues.)

> Can you please give an example code if possible?

There are examples here:

http://jdbc.postgresql.org/documentation/84/index.html

-Kevin

Re: Not able to connect to postgresql database

From
John R Pierce
Date:
Kevin Grittner wrote:
> Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:
>
>
>> shows an exception that it cannot find driver.
>>
> The jar is not on your classpath, or you are not spelling the class
> name ("org.postgresql.Driver") correctly.  I don't know of any other
> reason that the recommended class loading:
>
>     Class.forName("org.postgresql.Driver");
>
> would throw a ClassNotFoundException.  (If that's not what you're
> doing, or that's not the statement on which it fails, or that's not
> your exception, you need to give us more clues.)
>

a further clarification, you need to specify the full path and name of
the jdbc jar file in the classpath, its not enough to drop
postgresql-8.4-701.jdbc4.jar into a dir thats in the classpath as it
won't recognize the filename.

btw, you only need one of the jdbc drivers, you listed several different
ones.



Re: Not able to connect to postgresql database

From
Maciek Sakrejda
Date:
Well, first of all, you may want to stick with a single driver jar.
8.4 is backwards compatible with 8.3, so you should probably use 8.4,
and jdbc3 vs. jdbc4 is a Java version issue--jdbc4 for Java 6 and up,
jdbc3g for Java 5. I'm not sure what happens if you try to load
different definitions of the same class four times (not to mention
that I'm not sure how it's even possible, since the 3g and 4 versions
are compile-time incompatible with projects using a different Java
version).

If you're still getting errors, try an explicit

Class.forName("org.postgresql.Driver");

and see if you still get an Exception. If so, it sounds like an
Eclipse configuration issue since the jar isn't showing up in your
classpath.
---
Maciek Sakrejda | System Architect | Truviso

1065 E. Hillsdale Blvd., Suite 230
Foster City, CA 94404
(650) 242-3500 Main
(650) 242-3501 F
www.truviso.com



On Tue, Jun 1, 2010 at 1:19 PM, Manohar Bhattarai
<manoharbhattarai@gmail.com> wrote:
> Hi,
>  I am a newbie for postgresql. I am trying to connect to postgresql database
> using jdbc. I googled about it and got the Driver string and connection
> string formats. I tried using some examples i got on the internet. But my
> program is not able to connect to postgresql and shows an exception that it
> cannot find driver. I am doing it in Ubuntu8.04 Linux with Postgresql 8.3
> installed. I am using Eclipse galileo as IDE. I created database using
> commands in terminal. I have added the external jar files for postgresql
> jdbc connector(postgresql-8.3-603.jdbc3.jar, postgresql-8.3-603.jdbc4.jar,
> postgresql-8.4-701.jdbc3.jar and postgresql-8.4-701.jdbc4.jar) in my project
> also.
> Please help me and let me know what i am doing wrong and what i am missing.
> Can you please give an example code if possible?
> Thanks
> --
> Regards,
> Manohar Bhattarai (मनोहर भट्टराई)
> Blogs:
> http://manoharbhattarai.wordpress.com/
> http://manoharbhattarai.posterous.com/
> http://manoharbhattarai.blogspot.com/
> Microblogs:
> Twitter :- http://twitter.com/manoharmailme
> Identi.ca :- http://identi.ca/manoharbhattarai
>

Re: Not able to connect to postgresql database

From
Lew
Date:
On 06/01/2010 04:27 PM, Kevin Grittner wrote:
Manohar Bhattarai wrote:
>
>> shows an exception that it cannot find driver.
>
>> Please help me and let me know what i am doing wrong and what i am
>> missing.
>
> The jar is not on your classpath, or you are not spelling the class
> name ("org.postgresql.Driver") correctly.  I don't know of any other
> reason that the recommended class loading:
>
>      Class.forName("org.postgresql.Driver");

Just as an aside, nowadays the recommended way from Sun is to load a driver is
through DataSource descriptors, which has its advantages but is a lot less
simple at first.  With JPA you configure the data source in the
"persistence.xml" file for a local application, other XML files like
"context.xml" for app servers.

The 'Class.forName()' method is reliable but don't fall into the trap of
calling it more than once per program execution.

> would throw a ClassNotFoundException.  (If that's not what you're
> doing, or that's not the statement on which it fails, or that's not
> your exception, you need to give us more clues.)

All techniques require proper placement of the driver JAR file in your
classpath.  What that classpath is depends utterly on how you package and
deploy your app.

There's clear documentation on java.sun.com about JDBC, and about how to
specify the classpath for the "java ... foo.foo" ("-classpath" option) and
"java -jar foo.jar" (manifest file "Class-path:" entry) commands.  For the
various app servers you have to check the rules for the particular app server,
e.g.,
<http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html>

--
Lew

Re: Not able to connect to postgresql database

From
Manohar Bhattarai
Date:
Thanks for your replies.

Now i have removed other three jar files and have only one jar i.e. postgresql-8.4-701.jdbc4.jar as told above that it is backward compatible with Postgresql 8.3 and using jdk1.6. I have added the jar in classpath also in the CLASSPATH variable of /etc/environment file. Still I am getting exception : java.lang.ClassNotFoundException: org.postgresql.Driver.

A statement just above the line :
Class.forName("org.postgresql.Driver");
is executed and goes to the catch block.

Now what could be the issue? Still what am i doing wrong or missing?
Please help.

--
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai

Re: Not able to connect to postgresql database

From
"Kevin Grittner"
Date:
Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:

> I have added the jar in classpath also in the CLASSPATH variable
> of /etc/environment file. Still I am getting exception :
> java.lang.ClassNotFoundException: org.postgresql.Driver.

You said you were using Eclipse, which I believe sets its own
classpath for each runnable you set up within it.  Look for the
entry under the "Run..." or "Debug..." menu item and set it up
there.

-Kevin

Re: Not able to connect to postgresql database

From
dmp
Date:
Manohar Bhattarai <manoharbhattarai@gmail.com> wrote: 
> I have added the jar in classpath also in the CLASSPATH variable
> of /etc/environment file. Still I am getting exception :
> java.lang.ClassNotFoundException: org.postgresql.Driver.   
 
You said you were using Eclipse, which I believe sets its own
classpath for each runnable you set up within it.  Look for the
entry under the "Run..." or "Debug..." menu item and set it up
there.
-Kevin

If you are using Eclipse, and during the initial configuration of the project
the PostgreSQL driver was not present in the project directory then it will
needed to be added. To set that:

Highlight the Project:   Project  |  Properties  |  Java Build Path  |  Libraries  |  Add JARs.

danap.

Re: Not able to connect to postgresql database

From
Manohar Bhattarai
Date:


On Wed, Jun 2, 2010 at 9:09 PM, dmp <danap@ttc-cmc.net> wrote:
Manohar Bhattarai <manoharbhattarai@gmail.com> wrote: 
> I have added the jar in classpath also in the CLASSPATH variable
> of /etc/environment file. Still I am getting exception :
> java.lang.ClassNotFoundException: org.postgresql.Driver.   
 
You said you were using Eclipse, which I believe sets its own
classpath for each runnable you set up within it.  Look for the
entry under the "Run..." or "Debug..." menu item and set it up
there.
-Kevin

If you are using Eclipse, and during the initial configuration of the project
the PostgreSQL driver was not present in the project directory then it will
needed to be added. To set that:

Highlight the Project:   Project  |  Properties  |  Java Build Path  |  Libraries  |  Add JARs.

danap.
I had added the jar files already but it was giving same exception. But now the Driver is found. :) Thanks all. It was solved by adding the jar file to CATALINA_HOME/lib/ directory. Then it found the driver.
But now a new exception is :
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"

I am giving the line for connection that i am using
connection = DriverManager.getConnection(connectionURL, "postgres", "mypasswordhere");

But the password is the same that i use to login postgres user in the terminal.
What could be the problem?

Can you please give me the steps to set up new user and create new database after a fresh install of postgresql? I have doubts seeing different sites giving different methods.


Re: Not able to connect to postgresql database

From
"Kevin Grittner"
Date:
Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:

> now the Driver is found. :)

Good news.  :-)

> But now a new exception is :
> org.postgresql.util.PSQLException: FATAL: password authentication
> failed for user "postgres"

> But the password is the same that i use to login postgres user in
> the terminal.
> What could be the problem?

The password for the postgres *database* user is not necessarily the
same as for a postgres *OS* user.  You need to find or reset the
database user password.  If you need to reset it, you can do that by
modifying pg_hba.conf to allow trust or ident authentication (at
least temporarily) or by running the postmaster in single-user mode.

On the other hand, it is generally not a good idea to run the
application under the database superuser login; superuser logins
should be reserved for administrative purposes which require that
level of authority.  I generally use a non-superuser user for the
database owner and less privileged users for running applications.

> Can you please give me the steps to set up new user and create new
> database after a fresh install of postgresql? I have doubts seeing
> different sites giving different methods.

There are a lot of ways to do that, and what you should do next
probably depends on what you've done so far.  You're venturing into
territory which has nothing to do with JDBC -- you might want to
post this question on pgsql-admin or pgsql-general with a
description of what you've your OS, done so far, what you're trying
to do, and what problem you're hitting.

-Kevin

Re: Not able to connect to postgresql database

From
"Kevin Grittner"
Date:
[correcting copy/paste mangling]

I wrote:

> description of what you've your OS, done so far, what you're
> trying to do, and what problem you're hitting.

description of your OS, what you've done so far, what you're
trying to do, and what problem you're hitting.

-Kevin

Re: Not able to connect to postgresql database

From
Manohar Bhattarai
Date:


On Wed, Jun 2, 2010 at 10:11 PM, Kevin Grittner <Kevin.Grittner@wicourts.gov> wrote:
[correcting copy/paste mangling]

I wrote:

> description of what you've your OS, done so far, what you're
> trying to do, and what problem you're hitting.

description of your OS, what you've done so far, what you're
trying to do, and what problem you're hitting.

-Kevin

Thanks for your quick reply. I will be asking this problem as specified by you. Thank you all.

--
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai

Re: Not able to connect to postgresql database

From
Manohar Bhattarai
Date:


On Wed, Jun 2, 2010 at 11:36 PM, Manohar Bhattarai <manoharbhattarai@gmail.com> wrote:


On Wed, Jun 2, 2010 at 10:11 PM, Kevin Grittner <Kevin.Grittner@wicourts.gov> wrote:
[correcting copy/paste mangling]

I wrote:

> description of what you've your OS, done so far, what you're
> trying to do, and what problem you're hitting.

description of your OS, what you've done so far, what you're
trying to do, and what problem you're hitting.

-Kevin

Thanks for your quick reply. I will be asking this problem as specified by you. Thank you all.


Problem solved. All exception gone. :) Thanks all... for your quick help.
--
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai