繁体   English   中英

如何从命令行连接到在非默认端口上的 Docker 容器中运行的 Postgres?

[英]How can I connect to Postgres running in a Docker container on a non-default port from the command line?

我是 Docker 的新手,即使在阅读了有关 Stack Overflow 的几个类似问题之后,我也并不真正了解通过命令行使用psql连接到 Postgres 需要做什么。 我的 Postgres 容器在端口 6432 上运行,映射到默认端口 5432(我更改了默认端口,因为我还有 Postgres 桌面应用程序在端口 5432 上与其他数据库一起运行)。

我试过psql -p 6432但我一直收到这个错误: psql: error: could not connect to server: No such file or directory 然后我尝试了psql -h localhost -p 6432 -U root ,并被提示输入 root 用户的密码,即使我输入了正确的密码并产生了这个错误也失败了: psql: error: FATAL: database "root" does not exist 我知道我的 Postgres 容器正在运行,因为我可以看到它在 Docker 桌面应用程序中运行。

这是我的docker-compose.yml文件:

version: '3'

networks: 
    my_test_app:

services: 

    # nginx
    nginx-service:
        image: nginx:stable-alpine
        container_name: nginx-container
        ports: 
            - "8080:80"
        volumes: 
            - ./app:/var/www/project
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on: 
            - php-service
            - postgres-service
        networks: 
            - my_test_app 
  
    # php
    php-service:
        image: php:7.4-fpm-alpine
        container_name: php-container
        ports:
            - "9000:9000"
        working_dir: /var/www/project
        volumes:
            - ./app:/var/www/project
        networks: 
            - my_test_app  

    # postgres
    postgres-service:
        image: postgres:alpine
        container_name: postgres-container
        ports: 
            - "6432:5432"
        volumes: 
            - ./postgres:/var/lib/postgresql/data
        restart: always
        environment: 
            POSTGRES_USER: root
            POSTGRES_PASSWORD: secret
            POSTGRES_DB: db_my_test_app
        networks: 
            - my_test_app

...和我的 nginx 配置文件(我不知道这是否相关):

server {

    listen 80;
    index index.php index.html index.htm;
    server_name localhost;
    root /var/www/project/public;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass php-service:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }
}

我很感激你能为我提供的任何帮助。 顺便说一句,有什么方法可以查看和编辑我使用 Postico 之类的服务在此容器中创建的数据库(创建/更改表等)?

我认为问题在于您正在尝试连接到与实际存在的数据库不同的数据库。

您在配置中指定一个环境变量:

POSTGRES_DB: db_my_test_app

当这个 postgres 容器启动并找到一个空的数据目录时,它会为您创建一个数据库。 它根据一些检查确定新数据库的名称。 为了:

  1. $POSTGRES_DB的值是多少(如果已设置)
  2. `$POSTGRES_USER 的值是多少,如果设置了
  3. 如果没有别的,称之为'postgres'

psql连接到 postgres 时,它必须连接到数据库。 除非指定了不同的,否则它会尝试连接到与正在连接的用户同名的数据库。

给定您的命令: psql -h localhost -p 6432 -U root ,psql 将尝试连接到名为root的数据库。 当它发现这个数据库不存在时,它会打印以下内容:

❯ psql -h localhost -p 6432 -U root
Password for user root:
psql: error: FATAL:  database "root" does not exist

如果我们指定一个数据库名称来匹配在 docker-compose 配置中配置的名称,我们会得到以下信息:

✖2 ❯ psql -h localhost -p 6432 -U root -d db_my_test_app
Password for user root:
psql (13.2)
Type "help" for help.

db_my_test_app=#

您的快速解决方法是在尝试连接时指定-d <database_name>

如果您想更改此数据库的名称,“postgres 只有在找到空数据目录时才创建数据库”这一点变得很重要:因为您指定了 docker 卷./postgres:/var/lib/postgresql/data ,您的数据库文件将在 docker 撰写会话之间持久化,您需要删除主机上的./postgres目录以将其清除并创建一个新目录。

顺便说一句,由于您最初的问题表明psql无法连接到 postgres,我认为显示该错误消息的样子也会很有帮助:

❯ psql -h localhost -p 9876
psql: error: could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 9876?
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 9876?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM