如何使用 psycopg2 在 PostgreSQL 中创建 table?
How to create a table in PostgreSQL using pyscopg2?
我是 运行 一个 Docker Compose 应用程序,其中包含 postgres
作为一项服务。这是 docker-compose.yml
的一部分:
version: '3'
services:
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=foo
ports:
- 5432:5432
在 docker-compose build
之后是 docker-compose up
我看到容器是 运行 如果我这样做 docker ps
:
kurt@kurt-ThinkPad:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8a7a20686728 postgres "docker-entrypoint..." 28 minutes ago Up 17 seconds 0.0.0.0:5432->5432/tcp apkapi_postgres_1
由于端口 5432 映射到本地主机,我认为我可以执行以下操作(在 ipython
中):
In [1]: import psycopg2
In [2]: conn = psycopg2.connect(dbname="postgres", user="postgres", password="fo
...: o", host="localhost")
In [3]: cursor = conn.cursor()
In [4]: cursor.execute("CREATE TABLE apks (s3_uri text);")
所有这些命令执行无误。为了检查结果,我尝试了(使用 docker ps
中的容器 ID):
kurt@kurt-ThinkPad:~$ docker exec -it 8a7a20686728 bash
root@8a7a20686728:/# psql -U postgres
psql (9.6.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | mytable | table | postgres
(1 row)
问题是我看到了我之前创建的 table mytable
,但没有看到我尝试使用 [=24 创建的 table apks
=].我没有正确创建 table 吗?
听起来您可能没有启用自动提交,因此您对数据库所做的任何更改都不会被保存。尝试使用 conn.autocommit = True
或在执行命令后 conn.commit()
.
启用它
我是 运行 一个 Docker Compose 应用程序,其中包含 postgres
作为一项服务。这是 docker-compose.yml
的一部分:
version: '3'
services:
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=foo
ports:
- 5432:5432
在 docker-compose build
之后是 docker-compose up
我看到容器是 运行 如果我这样做 docker ps
:
kurt@kurt-ThinkPad:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8a7a20686728 postgres "docker-entrypoint..." 28 minutes ago Up 17 seconds 0.0.0.0:5432->5432/tcp apkapi_postgres_1
由于端口 5432 映射到本地主机,我认为我可以执行以下操作(在 ipython
中):
In [1]: import psycopg2
In [2]: conn = psycopg2.connect(dbname="postgres", user="postgres", password="fo
...: o", host="localhost")
In [3]: cursor = conn.cursor()
In [4]: cursor.execute("CREATE TABLE apks (s3_uri text);")
所有这些命令执行无误。为了检查结果,我尝试了(使用 docker ps
中的容器 ID):
kurt@kurt-ThinkPad:~$ docker exec -it 8a7a20686728 bash
root@8a7a20686728:/# psql -U postgres
psql (9.6.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | mytable | table | postgres
(1 row)
问题是我看到了我之前创建的 table mytable
,但没有看到我尝试使用 [=24 创建的 table apks
=].我没有正确创建 table 吗?
听起来您可能没有启用自动提交,因此您对数据库所做的任何更改都不会被保存。尝试使用 conn.autocommit = True
或在执行命令后 conn.commit()
.