Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple Java application that needs to connect to a PostgresSQL running on local host. The DB is up and running, and I can connect to it using the username and password using the psql command line utility. Here's the code:

try {
   Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException ex) {
   Logger.getLogger(DbLoader.class.getName()).log(Level.SEVERE, null, ex);
}

try ( 
    Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/db",
                    "user",
                    "pass");
 ){
// PROBLEM: con is null at this point
 } catch ( SQLException sqex ) {
    sqex.printStackTrace( System.out );
 }

Using Mac OS X.8.3. There is no exception thrown.

Here is the output of pg_config:

BINDIR = /Applications/Postgres.app/Contents/MacOS/bin
DOCDIR = /Applications/Postgres.app/Contents/MacOS/share/doc
HTMLDIR = /Applications/Postgres.app/Contents/MacOS/share/doc
INCLUDEDIR = /Applications/Postgres.app/Contents/MacOS/include
PKGINCLUDEDIR = /Applications/Postgres.app/Contents/MacOS/include
INCLUDEDIR-SERVER = /Applications/Postgres.app/Contents/MacOS/include/server
LIBDIR = /Applications/Postgres.app/Contents/MacOS/lib
PKGLIBDIR = /Applications/Postgres.app/Contents/MacOS/lib
LOCALEDIR = /Applications/Postgres.app/Contents/MacOS/share/locale
MANDIR = /Applications/Postgres.app/Contents/MacOS/share/man
SHAREDIR = /Applications/Postgres.app/Contents/MacOS/share
SYSCONFDIR = /Applications/Postgres.app/Contents/MacOS/etc
PGXS = /Applications/Postgres.app/Contents/MacOS/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/Users/mattt/Code/heroku/PostgresApp/Postgres/Vendor/postgres' '--enable-thread-safety' '--without-docdir' '--with-openssl' '--with-gssapi' '--with-bonjour' '--with-krb5' '--with-libxml' '--with-libxslt' '--with-perl' '--with-python'
CC = gcc
CPPFLAGS = -I/usr/include/libxml2 
CFLAGS = -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif- labels -Wformat-security -fno-strict-aliasing -fwrapv
CFLAGS_SL = 
LDFLAGS = -Wl,-dead_strip_dylibs
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgport -lxslt -lxml2 -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lm 
VERSION = PostgreSQL 9.1.4

Any ideas? Thanks in advance

share|improve this question
3  
Please post the stacktrace in order to get real help. –  Luiggi Mendoza May 17 '13 at 20:04
 
Are you catching exceptions? You should: try { ... } catch(Exception e){ e.printStackTrace(); } –  Barranka May 17 '13 at 20:08
 
By the way, don't you need a port to connect to your database? Looks like jdbc:postgresql://localhost/db needs it. –  Luiggi Mendoza May 17 '13 at 20:11
1  
@LuiggiMendoza: you don't need a port. If nothing is specified the driver will use the default port. But the question is impossible to answer without the actual error message. –  a_horse_with_no_name May 17 '13 at 22:10
 
@MichaelBarSinai: check out these answers: stackoverflow.com/search?q=postgres+osx+connection+problem –  a_horse_with_no_name May 17 '13 at 22:11
add comment

1 Answer

Got it - programmer (that's me!) error. I was passing the connection to another class, and that class had a null check that was not looking at the correct variables.

Takeaway - suspect yourself before you suspect PostgreSQL.

Thanks for all the comments! -- M

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.