代码之家  ›  专栏  ›  技术社区  ›  gongarek

Docker Compose中的数据库连接问题

  •  0
  • gongarek  · 技术社区  · 6 年前

    import psycopg2
    
    print("JJJJJJJJ")
    
    conn = psycopg2.connect("dbname='base123' user='postgres' host='db' password='pw1234'")
    cur = conn.cursor()
    cur.execute("CREATE TABLE test(id serial PRIMARY KEY, num int);")
    

    文档文件:

    FROM ubuntu:16.04
    
    RUN apt-get update 
    RUN apt-get -y install python-pip 
    RUN apt-get update
    RUN pip install --upgrade pip 
    RUN pip install psycopg2-binary
    
    COPY base.py base.py
    COPY wait-for-it.sh wait-for-it.sh
    RUN chmod +x /wait-for-it.sh
    CMD ["python","base.py"]
    

    version: '3'
    services:
      db:
        image: 'postgres:latest'
        expose:
          - "5432"
        ports:
        - "5559:5432"
         environment:
          POSTGRES_PASSWORD: pw1234
          POSTGRES_DB: base123
      aprrka:
        build: .
        depends_on:
          - db
    
        command: ["./wait-for-it.sh", "db:5432", "--", "python", "base.py"]
    

    wait-fot-it.sh号:

    link to code in github

    之后 docker-compose up 输出:

    ...
    Creating postgres_db_1 ... done
    Creating postgres_aprrka_1 ... done
    Attaching to postgres_db_1, postgres_aprrka_1
    db_1      | The files belonging to this database system will be owned by user "postgres".
    db_1      | This user must also own the server process.
    db_1      | 
    db_1      | The database cluster will be initialized with locale "en_US.utf8".
    db_1      | The default database encoding has accordingly been set to "UTF8".
    db_1      | The default text search configuration will be set to "english".
    db_1      | 
    db_1      | Data page checksums are disabled.
    db_1      | 
    db_1      | fixing permissions on existing directory /var/lib/postgresql/data ... ok
    db_1      | creating subdirectories ... ok
    db_1      | selecting default max_connections ... 100
    db_1      | selecting default shared_buffers ... 128MB
    db_1      | selecting dynamic shared memory implementation ... posix    
    aprrka_1  | wait-for-it.sh: waiting 15 seconds for db:5432
    db_1      | creating configuration files ... ok
    db_1      | running bootstrap script ... ok
    db_1      | performing post-bootstrap initialization ... ok
    db_1      | syncing data to disk ... ok
    db_1      | 
    db_1      | WARNING: enabling "trust" authentication for local connections
    db_1      | You can change this by editing pg_hba.conf or using the option -A, or
    db_1      | --auth-local and --auth-host, the next time you run initdb.
    db_1      | 
    db_1      | Success. You can now start the database server using:
    db_1      | 
    db_1      |     pg_ctl -D /var/lib/postgresql/data -l logfile start
    db_1      | 
    db_1      | waiting for server to start....2018-08-10 09:59:13.529 UTC [38] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    db_1      | 2018-08-10 09:59:13.582 UTC [39] LOG:  database system was shut down at 2018-08-10 09:59:12 UTC
    db_1      | 2018-08-10 09:59:13.599 UTC [38] LOG:  database system is ready to accept connections
    db_1      |  done
    db_1      | server started
    db_1      | CREATE DATABASE
    db_1      | 
    db_1      | ALTER ROLE
    db_1      | 
    db_1      | 
    db_1      | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
    db_1      | 
    db_1      | 2018-08-10 09:59:14.585 UTC [38] LOG:  received fast shutdown request
    db_1      | waiting for server to shut down....2018-08-10 09:59:14.593 UTC [38] LOG:  aborting any active transactions
    db_1      | 2018-08-10 09:59:14.613 UTC [38] LOG:  worker process: logical replication launcher (PID 45) exited with exit code 1
    db_1      | 2018-08-10 09:59:14.613 UTC [40] LOG:  shutting down
    db_1      | 2018-08-10 09:59:14.660 UTC [38] LOG:  database system is shut down
    db_1      |  done
    db_1      | server stopped 
    db_1      | 
    db_1      | PostgreSQL init process complete; ready for start up.
    db_1      | 
    db_1      | 2018-08-10 09:59:14.726 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
    db_1      | 2018-08-10 09:59:14.726 UTC [1] LOG:  listening on IPv6 address "::", port 5432
    db_1      | 2018-08-10 09:59:14.729 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    db_1      | 2018-08-10 09:59:14.806 UTC [65] LOG:  database system was shut down at 2018-08-10 09:59:14 UTC
    db_1      | 2018-08-10 09:59:14.829 UTC [1] LOG:  database system is ready to accept connections
    db_1      | 2018-08-10 09:59:15.217 UTC [72] LOG:  incomplete startup packet
    aprrka_1  | wait-for-it.sh: db:5432 is available after 5 seconds
    aprrka_1  | JJJJJJJJ
    postgres_aprrka_1 exited with code 0
    

    当我键入命令时: psql -U postgres -h localhost -p 5559 在另一个终端我可以连接到 base1234

    1 回复  |  直到 6 年前
        1
  •  1
  •   sachav    6 年前

    您需要关闭光标并 commit 将所有挂起的事务发送到数据库以使更改生效。

    在您的 base.py

    cur.close()
    conn.commit()