简体   繁体   English

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

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

I am new to Docker, and even after reading several similar questions on Stack Overflow do not really understand what I need to do to connect to Postgres using psql via the command line.我是 Docker 的新手,即使在阅读了有关 Stack Overflow 的几个类似问题之后,我也并不真正了解通过命令行使用psql连接到 Postgres 需要做什么。 My Postgres container is running on port 6432, mapped to the default port of 5432 (I changed the default port because I also have the Postgres desktop app running with other databases on port 5432).我的 Postgres 容器在端口 6432 上运行,映射到默认端口 5432(我更改了默认端口,因为我还有 Postgres 桌面应用程序在端口 5432 上与其他数据库一起运行)。

I have tried psql -p 6432 but I keep getting this error: psql: error: could not connect to server: No such file or directory .我试过psql -p 6432但我一直收到这个错误: psql: error: could not connect to server: No such file or directory I then tried psql -h localhost -p 6432 -U root , and was prompted to put in the password for the root user, which failed even though I input the correct password and produced this error: psql: error: FATAL: database "root" does not exist .然后我尝试了psql -h localhost -p 6432 -U root ,并被提示输入 root 用户的密码,即使我输入了正确的密码并产生了这个错误也失败了: psql: error: FATAL: database "root" does not exist I know that my Postgres container is running because I can see it running in the Docker desktop app.我知道我的 Postgres 容器正在运行,因为我可以看到它在 Docker 桌面应用程序中运行。

Here is my docker-compose.yml file:这是我的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

...and my nginx config file (I have no idea if this is relevant): ...和我的 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;
    }
}

I appreciate any help you may be able to offer me.我很感激你能为我提供的任何帮助。 By the way, is there any way that I can view and edit the databases (create/alter tables, etc.) I create in this container using a service like Postico?顺便说一句,有什么方法可以查看和编辑我使用 Postico 之类的服务在此容器中创建的数据库(创建/更改表等)?

I think the problem is that you're trying to connect to a different database than the one that actually exists.我认为问题在于您正在尝试连接到与实际存在的数据库不同的数据库。

You specify an environment variable in your config:您在配置中指定一个环境变量:

POSTGRES_DB: db_my_test_app

When this postgres container starts up and finds an empty data directory, it creates a database for you.当这个 postgres 容器启动并找到一个空的数据目录时,它会为您创建一个数据库。 It determines the name of that new database based on a few checks.它根据一些检查确定新数据库的名称。 In order:为了:

  1. What is the value of $POSTGRES_DB , if it is set $POSTGRES_DB的值是多少(如果已设置)
  2. What is the value of `$POSTGRES_USER, if it is set `$POSTGRES_USER 的值是多少,如果设置了
  3. If nothing else, call it 'postgres'如果没有别的,称之为'postgres'

When psql connects to postgres, it has to connect to a database.psql连接到 postgres 时,它必须连接到数据库。 Unless a different one is specified, it tries to connect to a database with the same name as the user that is connecting.除非指定了不同的,否则它会尝试连接到与正在连接的用户同名的数据库。

Given your command: psql -h localhost -p 6432 -U root , psql will try and connect to a database called root .给定您的命令: psql -h localhost -p 6432 -U root ,psql 将尝试连接到名为root的数据库。 When it finds that this database doesn't exist, it prints the following:当它发现这个数据库不存在时,它会打印以下内容:

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

If we specify a database name to match the one that's configured in your docker-compose configuration, we get the following:如果我们指定一个数据库名称来匹配在 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=#

Your quick fix is to specify -d <database_name> when you try to connect.您的快速解决方法是在尝试连接时指定-d <database_name>

If you'd like to change the name of this database, the bit about "postgres creates a database only if it finds an empty data directory" becomes important: since you specify a docker volume ./postgres:/var/lib/postgresql/data , your database files will be persisted between docker compose sessions and you'll need to delete the ./postgres directory on your host machine to clear it out and create a new one.如果您想更改此数据库的名称,“postgres 只有在找到空数据目录时才创建数据库”这一点变得很重要:因为您指定了 docker 卷./postgres:/var/lib/postgresql/data ,您的数据库文件将在 docker 撰写会话之间持久化,您需要删除主机上的./postgres目录以将其清除并创建一个新目录。

As an aside, since your initial question states that psql is unable to connect to postgres, I think it'll be helpful to show what that error message looks like as well:顺便说一句,由于您最初的问题表明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.

相关问题 如何将不在Docker上运行的Odoo连接到在Docker上运行的Postgres容器? - How can I connect Odoo not running on Docker to a Postgres container running on Docker? 如何通过端口5432连接到容器中的Postgres数据库 - How can I connect to Postgres database in the container via port 5432 为 Homebrew Postgres 服务器指定非默认端口 - Specifying non-default port for Homebrew Postgres server 无法从Golang连接到docker postgres容器 - Can not connect from golang to docker postgres container 如何从外部 CLI 连接到 Docker Postgres 容器? - How do I connect to Docker Postgres Container from an Outside CLI? 如何通过本地运行的 psql 命令连接到在本地 Docker 容器中运行的 Postgres 数据库? - How to connect to a Postgres database running in local Docker container through locally-run psql command? 在 Docker 容器中运行 .Net 5 API 时无法连接到 Postgres - Can't connect to Postgres when running .Net 5 API in Docker container 无法从 Golang 容器连接到 Postgres docker 容器 - Can't connect to Postgres docker container from Golang container 如何连接到OSx上的Docker容器中运行的postgres? - How do you connect to postgres running in a Docker container on OSx? 只能将 pgadmin 连接到端口 5432 上的 postgres docker 容器,但不能连接任何其他端口? - Can only connect pgadmin to postgres docker container on port 5432, but not any other port?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM