Skip to content

psql -h localhost fails in init script #474

Closed
@simonseyock

Description

psql -h localhost fails in init scripts. I encountered this problem while running a third party script I can not change.

Simple Dockerfile to reproduce:

FROM postgres:9.4
COPY ./init.sh /docker-entrypoint-initdb.d/
RUN chmod a+x /docker-entrypoint-initdb.d/init.sh

init.sh:

#!/bin/bash
psql -h localhost -p 5432 -U postgres -d postgres -c 'select 1;'

Output:

psql: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?

I tried this on windows and linux and with and without the chmod line. The crucial part in the psql command is the -h localhost option. All other options can be removed and the error still occurs, but if the -h is removed everything works fine.

Does a workaround exist for this?

Metadata

Assignees

No one assigned

    Labels

    questionUsability question, not directly related to an error with the image

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions