2

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

user3909893
  • 421
  • 1
  • 4
  • 15

1 Answers1

0

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.

Rich Duncan
  • 1,845
  • 1
  • 12
  • 13
  • What do you mean by saying "you are trying to use shell command syntax and you're not running a shell"? I have another similar executions within shell, like ` def command = "aws ecr list-images"` and `def proc = "git ls-remote -h ${gitURL}".execute()` They run with success – user3909893 Jul 11 '19 at 17:51
  • In the above command, the string substituion (for gitURL) is happening in Groovy. I'm assuming the psql command will look for a PGPASSWORD environment varaible. In order to set this you need to create a shell script and execute that, so the command would be 'sh -c ....'. In which case you can use a shell step which would be easier – Rich Duncan Jul 11 '19 at 18:43