我在服务器上的docker容器中有Postgresql。我如何从外部,也就是从我的本地计算机连接到它?我应该应用什么设置来允许这样做?
当前回答
我设法让它在linux上运行
运行docker postgres -确保端口被发布,我使用alpine是因为它是轻量级的。 docker run——rm -P 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD="1234"——name pg postgres:alpine 使用另一个终端,使用postgres uri从主机访问数据库 psql postgresql: / / postgres: 1234 @localhost: 5432 / postgres
MAC用户请将PSQL替换为pgcli
其他回答
连接到运行postgres的本地容器
安装 psql
酿造搜索postgres
编译安装postgresql
2.
docker run --name postgres -e POSTGRES_DB=users \
-e POSTGRES_USER=john \
-e POSTGRES_PASSWORD=password \
-p 5432:5432 -d postgres
psql --host=localhost --username=john --dbname=users
我尝试从localhost (mac)连接到postgres容器。我将docker-compose文件中的端口从5432更改为3306,并启动了容器。不知道我为什么这么做:|
然后我尝试通过PSequel和adminer连接到postgres,但连接无法建立。
切换回端口5432后,一切工作正常。
db:
image: postgres
ports:
- 5432:5432
restart: always
volumes:
- "db_sql:/var/lib/mysql"
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres_db
这就是我想要分享的经验。也许有人可以利用它。
如果它是一个django后端应用程序,你可以这样做。
docker exec -it container_id python manage.py dbshell
由于某种原因,5432端口似乎受到保护。我把我的端口配置从5432:5432更改为5416:5432,下面的命令可以从docker容器外部连接到postgres数据库:
psql -h localhost -p 5416 -U <my-user> -d <my-database>
这个对我很有用:
PGPASSWORD=postgres psql -h localhost -p 3307 -U postgres -d postgres
使用上面的方法加载一个初始脚本:
PGPASSWORD=postgres psql -h localhost -p 3307 -U postgres -d postgres < src/sql/local/blabla.sql
不要说我重新映射我的端口为:
docker run -p3307:5432 --name postgres -e POSTGRES_PASSWORD=postgres -d postgres
推荐文章
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- 纬度和经度的数据类型是什么?
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 试图连接到https://index.docker.io时,网络超时
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何在Postgres中获得两个字段的MIN() ?
- 为每个Docker图像查找图层和图层大小
- 如何避免在为Python项目构建Docker映像时重新安装包?
- 截断Postgres数据库中的所有表
- 如何用docker-compose更新现有图像?