Search code examples
chef-infrachef-recipe

Identify PostgreSQL version using chef recipes


Is it possible to identify which PostgreSQL version is running on Database Server inside the recipes in chef.

right now i am able to identify it using awk command

example:

[postgres@hostname]$ ps -aux  | grep postgres
psharm89 39532  0.0  0.0 112644   960 pts/2    S+   12:31   0:00 grep -- color=auto postgres
postgres 56958  0.0  0.9 2345396 77444 ?       S    May21   1:10 /opt/postgres/9.5/bin/postgres -D /opt/pgdata/9.5/data
 postgres 56959  0.0  0.0 166416  1836 ?        Ss   May21   0:57 postgres: logger process
 postgres 56961  0.0  2.1 2347908 173768 ?      Ss   May21   0:45 postgres: checkpointer process
 postgres 56962  0.0  0.2 2345412 21096 ?       Ss   May21   0:14 postgres: writer process
 postgres 56963  0.0  0.2 2345352 18284 ?       Ss   May21   0:25 postgres: wal writer process
 postgres 56964  0.0  0.0 2345788 2820 ?        Ss   May21   1:14 postgres: autovacuum launcher process
 postgres 56965  0.0  0.0 168668  2156 ?        Ss   May21   2:22 postgres: stats collector process

[postgres@hostname]$ PGHOME=$(ps -aux  | grep postgres | grep -- -D |grep -v grep |awk '{NF=NF-2;print $NF}'|awk -F"/bin" '{print $1}')
[postgres@hostname]$ PGVER=$(/bin/basename $PGHOME)
[postgres@hostname]$ echo $PGVER
 9.5

but, similarly i need to find-out inside the recipes in chef so that i can create variable PGVER=9.5 and used it inside my postgresql database configuration recipes.


Solution

  • This is generally not how you would use Chef. Chef would be the thing that enforces which version of Postgres is in use.

    But if you must, you can use something like this:

    pgver = shell_out!('ps -aux').stdout[%r{postgres/(9\.\d+)/bin}, 1]
    

    That mostly just moves the string parsing into Ruby rather than Bash, but same basic idea.