Home > OS >  Non-interactive postgres command hangs in Ubuntu machine
Non-interactive postgres command hangs in Ubuntu machine

Time:10-15

I'm creating a CircleCI config file with the following excerpt:

executors:
    ubuntu:
        machine:
            image: ubuntu-2004:202107-02
jobs:
    build:
        executor: ubuntu
        environment:
            PGUSER: postgres
            DATABASE_URL: postgresql://postgres@localhost:5432/postgres
            TERM: xterm
        steps:
            - checkout
            - run: sudo apt-get update
            - run: sudo apt-get install postgresql
            - run: psql -V
            - run: pg_isready
            - run: |
                psql \
                -d $DATABASE_URL \
                -c "CREATE TABLE test (name char(25));"
            - run: |
                psql \
                -d $DATABASE_URL \
                -c "INSERT INTO test VALUES ('John'), ('Joanna'), ('Jennifer');"
            - run: |
                psql \
                -d $DATABASE_URL \
                -c "SELECT * from test;"

All commands prior to and including pg_isready exit without error, but no matter what I try the next command (CREATE TABLE) hangs. The CircleCI log displays this with no further output:

#!/bin/bash -eo pipefail
psql \
-d $DATABASE_URL \
-c "CREATE TABLE test (name char(25));"

I'm using code taken from CircleCI's documentation with minor modifications: https://circleci.com/docs/2.0/databases/

This type of work is new to me – I'm hoping it's an obvious missing command – thanks in advance!

CodePudding user response:

Posting my answer in case others encounter this: My specific issue was that psql kept going into interactive mode. This occurred for any psql commands – not just password request. This prevented the process from exiting and continuing to the next.

I was able to solve the issue for CircleCI by piping my command to cat. For example:

            - run:
                name: Create test database
                command: sudo -u postgres createdb testdb | cat

Additionally, turning off the page option worked as well, but seemed more verbose: psql -P pager=off

Please add to this thread if you find a better way! (I tried environment vars but couldn't get them to stick.)

  • Related