无法从Docker连接到Postgres

时间:2019-10-22 10:06:43

标签: postgresql docker hadoop hbase

我有一个docker映像,其中有一个由一个主设备和两个从设备组成的hadoop集群。在这个群集上,我有HBase。

我正在尝试使用scoop将安装在本地计算机上的postgres数据库迁移到Docker映像上的HBase数据库。

对于postgres,这是我的postgresql.conf配置文件:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart) port = 5432                             # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart) unix_socket_directories = '/var/run/postgresql' # comma-separated list
of directories
                                        # (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
                                        # (change requires restart)
#bonjour = off                          # advertise server via Bonjour
                                        # (change requires restart)
#bonjour_name = ''                      # defaults to the computer name


# - Security and Authentication -

#authentication_timeout = 1min          # 1s-600s ssl = on
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
#ssl_prefer_server_ciphers = on
#ssl_ecdh_curve = 'prime256v1'
#ssl_dh_params_file = '' ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem' ssl_key_file =
'/etc/ssl/private/ssl-cert-snakeoil.key'
#ssl_ca_file = ''
#ssl_crl_file = ''
#password_encryption = md5              # md5 or scram-sha-256
#db_user_namespace = off
#row_security = on

# GSSAPI using Kerberos
#krb_server_keyfile = ''
#krb_caseins_users = off

# - TCP Keepalives -
# see "man 7 tcp" for details

#tcp_keepalives_idle = 0                # TCP_KEEPIDLE, in seconds;
                                        # 0 selects the system default
#tcp_keepalives_interval = 0            # TCP_KEEPINTVL, in seconds;
                                        # 0 selects the system default
#tcp_keepalives_count = 0               # TCP_KEEPCNT;
                                        # 0 selects the system default

这也是pg_hba.conf文件的内容:

# Database administrative login by Unix domain socket local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only local   all             all                                     trust
# IPv4 local connections: host    all             all             127.0.0.1/32            trust
# IPv6 local connections: host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege. 
local   replication     all                              trust 

host    replication     all             127.0.0.1/32     trust      
host    replication     all             ::1/128             

我的问题是,当我尝试使用以下命令使用独家新闻从Docker映像连接到Postgres时:

sqoop import --connect jdbc:postgresql://localhost:5432/mimic --username postgres --password 0000 --table admission_ids --hbase-table mimic --column-family admission_ids --hbase-row-key id -m 1

我只是有这个问题:

Check that the hostname and port are correct and that the postmaster
is accepting TCP/IP connections. org.postgresql.util.PSQLException:
Connection refused. Check that the hostname and port are correct and
that the postmaster is accepting TCP/IP connections.

搜索后,我了解到docker是此问题的根源,并确认这一点,我只是第一次尝试使用本地计算机上的tenlnet连接到postgres,这是结果:

telnet localhost 5432
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

第二次从docker上

telnet localhost 5432
Trying 127.0.0.1...
Trying ::1...
telnet: Unable to connect to remote host: Cannot assign requested address

我的docker映像名为spark-hadoop。

我试图截断此命令,但始终无法正常工作

docker run -d --name bridgeToHadoop --publish=127.0.0.1:5432:5432 -p 172.18.0.2:5432:5432 spark-hadoop

1 个答案:

答案 0 :(得分:0)

在主机上运行的Postgresql,您正尝试从Docker容器连接它。执行此操作时,主机不应在连接字符串中为{ "_id" : "7XXXXXXXXXX", "apps" : [ { "id" : "e0d538e0df9a345e", "os" : "android", "token" : "f1zp-VSi7Ec:APA91bEAbfH8nMeVidkIjPrJ28WHRFDy-BhvKCQdDSdCYsylUzET9GjbPHjofCrr1NMQurinMCI4fuiF7VWNPXne-80op_h0MloK217kc1zKptdo9FTgAH5R932uDphcyB1xQ27-AFdR", "version" : "3.2.1", "build" : "8115680e", "timestamp" : NumberLong(1571740696818) } ] } 。本地主机是指Docker容器。

用IP地址替换本地主机,应该紧随其后

localhost