【问题标题】:When I try to insert data into my posgresql database nothing happens当我尝试将数据插入我的 postgresql 数据库时,没有任何反应
【发布时间】:2021-03-19 16:34:44
【问题描述】:

我先说几天前我开始使用 python、docker 等,我有点迷路了。

我已经学习了多个教程,并且能够将它们放在一起。

我在 docker 上有一个 postgresql 数据库,在端口 5432 上运行,我的 app.py 在端口 5000 上运行。 我创建了一个简单的网站,其中包含我想将输入发送到数据库的表单。但是,当按下提交按钮时没有任何反应。本地主机只是不断加载和加载。 cmd中没有错误,什么都没有。 我的数据库名为 height_collector,我创建了一个包含 id、email 和 height 列的反馈表。

app.py:

import os
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:mysecretpassword@localhost:5432/height_collector'

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

if __name__ == '__main__':
    app.debug=True
    app.run()

如果有人可以查看它并可能发现错误,或者如果您有关于如何将我的 py 应用程序连接到 docker 上的 postgress 数据库的良好教程的推荐,我将非常高兴。谢谢你的帮助。

Docker 记录 postgres:

D:\ZCT_zadanie>docker logs postgres
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
sh: locale: not found
2021-03-19 18:25:51.752 UTC [27] WARNING:  no usable system locales were found
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start


WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
waiting for server to start....2021-03-19 18:25:53.304 UTC [31] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-03-19 18:25:53.348 UTC [32] LOG:  database system was shut down at 2021-03-19 18:25:52 UTC
2021-03-19 18:25:53.357 UTC [31] LOG:  database system is ready to accept connections
 done
server started
ALTER ROLE


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

waiting for server to shut down....2021-03-19 18:25:53.400 UTC [31] LOG:  received fast shutdown request
2021-03-19 18:25:53.405 UTC [31] LOG:  aborting any active transactions
2021-03-19 18:25:53.407 UTC [31] LOG:  worker process: logical replication launcher (PID 38) exited with exit code 1
2021-03-19 18:25:53.407 UTC [33] LOG:  shutting down
2021-03-19 18:25:53.460 UTC [31] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2021-03-19 18:25:53.523 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2021-03-19 18:25:53.523 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2021-03-19 18:25:53.532 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-03-19 18:25:53.567 UTC [42] LOG:  database system was shut down at 2021-03-19 18:25:53 UTC
2021-03-19 18:25:53.578 UTC [1] LOG:  database system is ready to accept connections
2021-03-19 18:26:17.065 UTC [49] LOG:  invalid length of startup packet
2021-03-19 18:26:18.150 UTC [50] LOG:  invalid length of startup packet
2021-03-19 18:26:23.219 UTC [51] LOG:  invalid length of startup packet
2021-03-19 18:26:43.049 UTC [52] LOG:  invalid length of startup packet
2021-03-19 18:26:44.144 UTC [53] LOG:  invalid length of startup packet
2021-03-19 18:26:49.209 UTC [54] LOG:  invalid length of startup packet
2021-03-19 18:26:57.697 UTC [56] LOG:  invalid length of startup packet
2021-03-19 18:26:58.939 UTC [57] LOG:  invalid length of startup packet
2021-03-19 18:27:19.041 UTC [58] LOG:  invalid length of startup packet
2021-03-19 18:27:25.218 UTC [59] LOG:  invalid length of startup packet
2021-03-19 18:27:50.702 UTC [60] LOG:  invalid length of startup packet
2021-03-19 18:27:55.976 UTC [62] LOG:  invalid length of startup packet
2021-03-19 18:27:56.288 UTC [63] LOG:  invalid length of startup packet
2021-03-19 18:28:05.209 UTC [64] LOG:  invalid length of startup packet
2021-03-19 18:28:06.297 UTC [65] LOG:  invalid length of startup packet
2021-03-19 18:28:11.363 UTC [66] LOG:  invalid length of startup packet
2021-03-19 18:28:20.625 UTC [67] LOG:  invalid length of startup packet
2021-03-19 18:28:21.316 UTC [68] LOG:  invalid length of startup packet
2021-03-19 18:28:21.514 UTC [69] LOG:  invalid length of startup packet
2021-03-19 18:28:21.685 UTC [70] LOG:  invalid length of startup packet
2021-03-19 18:28:21.826 UTC [71] LOG:  invalid length of startup packet
2021-03-19 18:28:21.998 UTC [72] LOG:  invalid length of startup packet
2021-03-19 18:28:22.194 UTC [73] LOG:  invalid length of startup packet
2021-03-19 18:28:22.382 UTC [74] LOG:  invalid length of startup packet
2021-03-19 18:28:22.556 UTC [75] LOG:  invalid length of startup packet
2021-03-19 18:28:22.868 UTC [76] LOG:  invalid length of startup packet
2021-03-19 18:28:23.051 UTC [77] LOG:  invalid length of startup packet
2021-03-19 18:28:23.244 UTC [78] LOG:  invalid length of startup packet
2021-03-19 18:28:23.460 UTC [79] LOG:  invalid length of startup packet
2021-03-19 18:28:53.535 UTC [80] LOG:  invalid length of startup packet
2021-03-19 18:29:40.472 UTC [82] LOG:  invalid length of startup packet
2021-03-19 18:32:22.609 UTC [86] LOG:  invalid length of startup packet
2021-03-19 18:32:23.702 UTC [87] LOG:  invalid length of startup packet
2021-03-19 18:33:58.625 UTC [90] LOG:  invalid length of startup packet
2021-03-19 18:33:59.873 UTC [91] LOG:  invalid length of startup packet
2021-03-19 18:34:04.920 UTC [92] LOG:  invalid length of startup packet
2021-03-19 18:35:44.469 UTC [94] LOG:  invalid length of startup packet
2021-03-19 18:35:45.580 UTC [95] LOG:  invalid length of startup packet
2021-03-19 18:36:01.821 UTC [97] LOG:  invalid length of startup packet
2021-03-19 18:40:08.252 UTC [102] LOG:  invalid length of startup packet
2021-03-19 18:40:09.357 UTC [103] LOG:  invalid length of startup packet
2021-03-19 18:43:34.687 UTC [107] LOG:  invalid length of startup packet
2021-03-19 18:43:35.351 UTC [108] LOG:  invalid length of startup packet
2021-03-19 18:43:35.543 UTC [109] LOG:  invalid length of startup packet
2021-03-19 18:43:35.722 UTC [110] LOG:  invalid length of startup packet

Docker-组合起来:

D:\ZCT_zadanie>docker-compose up
Starting zct_zadanie_postgres_1 ... done
Starting zct_zadanie_pgadmin_1  ... done
Attaching to zct_zadanie_postgres_1, zct_zadanie_pgadmin_1
postgres_1  | 2021-03-19 19:20:28.074 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1  | 2021-03-19 19:20:28.075 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1  | 2021-03-19 19:20:28.084 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2021-03-19 19:20:28.139 UTC [19] LOG:  database system was shut down at 2021-03-19 19:20:22 UTC
postgres_1  | 2021-03-19 19:20:28.151 UTC [1] LOG:  database system is ready to accept connections
postgres_1  | 2021-03-19 19:20:32.106 UTC [26] LOG:  invalid length of startup packet
postgres_1  | 2021-03-19 19:20:37.205 UTC [27] LOG:  invalid length of startup packet
pgadmin_1   | [2021-03-19 19:20:39 +0000] [1] [INFO] Starting gunicorn 20.0.4
pgadmin_1   | [2021-03-19 19:20:39 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
pgadmin_1   | [2021-03-19 19:20:39 +0000] [1] [INFO] Using worker: threads
pgadmin_1   | [2021-03-19 19:20:39 +0000] [82] [INFO] Booting worker with pid: 82
postgres_1  | 2021-03-19 19:20:48.130 UTC [28] LOG:  invalid length of startup packet
postgres_1  | 2021-03-19 19:21:17.382 UTC [29] LOG:  invalid length of startup packet
Gracefully stopping... (press Ctrl+C again to force)
Stopping zct_zadanie_pgadmin_1  ... done
Stopping zct_zadanie_postgres_1 ... done

【问题讨论】:

  • 你能分享你的 dockerfile 和 docker run 命令吗?
  • 我还没有学会如何编写 Dockerfile。但这些是我用来设置它的命令。 docker network create pgnet docker volume create pgdata docker run ^ --name postgres ^ --network pgnet ^ -v E:\docker_volume:/data ^ -e POSTGRES_PASSWORD=mysecretpassword ^ -d postgres:10.4-alpine 现在想想,我应该创建 Dockerfile,不是吗?这是我应该遵循的 Dockerfile 的一个很好的例子吗? testdriven.io/blog/…
  • 你不需要 db 的 dockerfile。
  • 我似乎很好,只需在 docker-compose up 命令之后等待,localhost:8080/login 将是您的运行页面,之后您可以按照步骤 3 进行操作。
  • 而且只有 docker-compose 容器才能确保清晰。不是任何其他 postgres。

标签: python postgresql docker


【解决方案1】:

让我们有个明确的开始。

  1. 杀死所有容器

    docker kill $(docker ps -q)

  2. 将此命令复制到名为 docker-compose.yaml 的文件中 然后运行命令 docker-compose up (如果没有安装请检查link)


version: '3'
services:
  postgres:
    image: postgres:10.4-alpine
    ports:
      - "5446:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    volumes:
      - postgres-data:/data
    networks:
      - app-network

  pgadmin:
    image: dpage/pgadmin4
    depends_on:
      - postgres
    ports:
      - "8080:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: pgadmin4@pgadmin.org
      PGADMIN_DEFAULT_PASSWORD: admin
    networks:
      - app-network

volumes:
  postgres-data:
networks:
  app-network:
    driver: bridge
  1. 添加新服务器 ->

Host: postgres
Port: 5432
db: postgres
user: postgres
pasword: postgres

【讨论】:

  • 我收到此错误:docker:来自守护进程的错误响应:驱动程序在端点 postgres 上编程外部连接失败(a90ca8cded1c5fcadeaf4c3d87e3eb911145e23a0c9d33a8c1ac2d7cc9ed99da):绑定 0.0.0.0:5432 失败:端口已分配。我应该更改端口的第一部分还是第二部分以分配新的?
  • 在您的 pc 5432 上用于另一个应用程序。您可以指定另一个。 -p 5433:5432 5433 将在您的机器上。在你的 db_uri 中也使用 5433
  • 我一直收到同样的错误。我尝试了 5433 端口,也尝试了 8001,但同样的情况发生了
  • 这与您的本地端口有关。尝试一种在您的本地未使用的。 5000 - 5444 - 5*** 的东西
  • 我应该在同一个端口上运行 pgadmin 吗?例如在 5444:5432 上运行 pgadmin,在 5444:5432 上运行 postgres?因为无论我从 5 尝试什么端口......我都会遇到同样的错误。现在我运行它是这样的:docker run -p 5446:5432 --name pgadmin --network pgnet -e "PGADMIN_DEFAULT_EMAIL=user@domain.com" -e "PGADMIN_DEFAULT_PASSWORD=SuperSecret" -d dpage/pgadmin4docker run --name postgres --network pgnet -p 5446:5432 -v D:\docker_volume:/data -e POSTGRES_PASSWORD=mysecretpassword -d postgres:10.4-alpine
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多