You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker部署PostgreSQL容器后用户无法识别的问题求助

Fixing the psql Error in Your PostgreSQL Docker Container

Hey there! Let's break down why your psql command isn't working and how to fix it quickly.

The Root Causes of the Error

Your command psql -u postgres -p has a few key issues that are triggering the exception:

  1. Invalid username flag: psql uses uppercase -U to specify a username, not lowercase -u (that's not a valid psql parameter at all).
  2. Incomplete port parameter: The -p flag requires a port number to follow it. Since PostgreSQL uses port 5432 by default in the official container, you can either omit -p entirely or explicitly add -p 5432.
  3. Username mismatch: You launched the container with POSTGRES_USER=root, which means the superuser account created in the database is root—not the default postgres user (this default gets replaced when you set the POSTGRES_USER environment variable).

Correct Commands to Use

Option 1: Log in with your configured superuser (root)

Once you're inside the container via docker exec -it postgres /bin/bash, run this valid command:

psql -U root

If you want to explicitly specify the default port (though it's unnecessary), use:

psql -U root -p 5432

Option 2: Skip the bash shell entirely

You can run psql directly from your host machine without entering the container first—this saves a step:

docker exec -it postgres psql -U root

If you really need the postgres user

If you prefer using the traditional postgres superuser instead of root, you have two options:

  1. Create the postgres user after logging in as root:
    CREATE USER postgres WITH SUPERUSER PASSWORD 'your-preferred-password';
    
    Then log out and run psql -U postgres to switch over.
  2. Recreate the container without setting the POSTGRES_USER variable—this will default to using postgres as the superuser.

内容的提问来源于stack exchange,提问作者user8495266

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:50:24