Can not execute connection to Postgres via Jenkins Groovy

2.2k Views Asked by At

Using Scriptler can not connect to Postgresql via Jenkins groovy, neither via executing psql command, nor via using jdbc:

  1. psql
command = """
        PGPASSWORD=1111\
        psql -h xxxx.rds.amazonaws.com\
        -U master -d yyy -c "select * from table"
        """
proc =  command.execute()
proc.waitFor()
return proc.in.text 

I receive the error

Cannot run program "PGPASSWORD=1111": error=2, No such file or directory

  1. jdbc
import groovy.sql.Sql

def dbUrl      = "jdbc:postgresql://xxxx.rds.amazonaws.com/yyy"
def dbUser     = "master"
def dbPassword = "1111"
def dbDriver   = "org.postgresql.jdbcDriver"
def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)

it returns

java.lang.ClassNotFoundException: org.postgresql.jdbcDriver

I installed plugins database, PostgreSQL API Plugin & database-postgresql. Jenkins v.2.176.1

1

There are 1 best solutions below

2
Rich Duncan On

So your first attempt via command.execute() will not work because you are trying to use shell command syntax and you're not running a shell.

The 2nd one will not work because you have to tell Groovy where to find the postgress jdbc library. You may be able to do this with Groovy Grape.

Personally I would do psql command using a shell step.