Nc command not found, psql command not found

Hey guys, I am new to CircleCI and the CircleCI community. Thank you for having me. I am trying to set up a run command that waits to see if postgresql is listening on its default port. I keep getting nc not found. I tried ‘which nc -z …’ that seemed to rid of the command not found error but it still fails when postgres is booted and waiting for connections. although the wait circle still spins next to ‘Container postgres…’ step.

Heres my config.yml:

version: 2.1
jobs:
  build:
    docker:
      - image: node:12
      - image: circleci/postgres:11-alpine-ram
        environment:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: testdb

    working_directory: ~/repo

    steps:
      - checkout
      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run: yarn install

      - save_cache:
          paths:
          - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      - run:
          name: Waiting for Postgres to be ready
          command: |
            for i in `seq 1 10`;
            do
              nc -z localhost 5432 && echo Success && exit 0
              echo -n .
              sleep 1
            done
            echo Failed waiting for Postgres && exit 1
    # run tests!
      - run: yarn ci:test

my postgresql output:
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /dev/shm/pgdata/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... sh: locale: not found
2020-07-29 22:58:44.456 UTC [49] WARNING:  no usable system locales were found
ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /dev/shm/pgdata/data -l logfile start

waiting for server to start....2020-07-29 22:58:44.821 UTC [94] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-07-29 22:58:44.830 UTC [99] LOG:  database system was shut down at 2020-07-29 22:58:44 UTC
2020-07-29 22:58:44.831 UTC [94] LOG:  database system is ready to accept connections
 done
server started
CREATE DATABASE


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

waiting for server to shut down....2020-07-29 22:58:45.133 UTC [94] LOG:  received fast shutdown request
2020-07-29 22:58:45.133 UTC [94] LOG:  aborting any active transactions
2020-07-29 22:58:45.134 UTC [94] LOG:  background worker "logical replication launcher" (PID 105) exited with exit code 1
2020-07-29 22:58:45.134 UTC [100] LOG:  shutting down
2020-07-29 22:58:45.136 UTC [94] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2020-07-29 22:58:45.238 UTC [7] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-07-29 22:58:45.238 UTC [7] LOG:  listening on IPv6 address "::", port 5432
2020-07-29 22:58:45.238 UTC [7] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-07-29 22:58:45.247 UTC [111] LOG:  database system was shut down at 2020-07-29 22:58:45 UTC
2020-07-29 22:58:45.248 UTC [7] LOG:  database system is ready to accept connections

And step Waiting for Postgres:

#!/bin/bash -eo pipefail
for i in seq 1 10;
do
nc -z localhost 5432 && echo Success && exit 0
echo -n .
sleep 1
done
echo Failed waiting for Postgres && exit 1
/bin/bash: line 2: nc: command not found
./bin/bash: line 2: nc: command not found
./bin/bash: line 2: nc: command not found
./bin/bash: line 2: nc: command not found
.
/bin/bash: line 2: nc: command not found
.
/bin/bash: line 2: nc: command not found
.
/bin/bash: line 2: nc: command not found
.
/bin/bash: line 2: nc: command not found
.
/bin/bash: line 2: nc: command not found
.
/bin/bash: line 2: nc: command not found
.
Failed waiting for Postgres

Exited with code exit status 1
CircleCI received exit code 1
Any ideas where I am off on this?

1 Like

So I fixed this problem by creating a custom image. https://circleci.com/docs/2.0/custom-images/
I didn’t recreate the docs image but took a simple custom image from their github demo that’s on the same page and adjusted it to my use case. Held my breath… and it worked. Tada. Fixed the nc error and made it possible for me to psql -f create.sql My Dockerfile for the custom image is configured this way:

FROM node:12.0.0

RUN apt-get update && apt-get install -y netcat

RUN apt-get install -y postgresql-client

this helped me login, tag, and push my image file after i built it: dockerfile - denied: requested access to the resource is denied: docker - Stack Overflow

So now the wait for postgres works and I can create and seed my database.

  - run:
      name: Create tables
      command: psql -h localhost -U postgres -d testdb -f src/db/create.sql
  - run:
      name: Seed database
      command: psql -h localhost -U postgres -d testdb -f src/db/seed.sql
1 Like

Hi,

Welcome to CircleCI Discuss!

I’m happy to see you were able to come up with a solution, but for anyone that may find this in the future, there is a solution that does not require creating custom images.

We provide convenience images with pre-installed tools, such as netcat (nc). You can find our node convenience images here. If you are looking for another image, please refer to our convenience image documetation. Within that documentation, you can find the list of tools that are pre-installed.

Instead of - image: node:12 use - image: circleci/node:12 and you will then have access to nc.

If you have any more questions, please let me know!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.