如何将Python连接到PostgreSQL

如何将Python连接到PostgreSQL,python,postgresql,Python,Postgresql,在Google上,我找到了关于PyGreSQL库的文档,这些文档可以帮助我将Python连接到Postgres 但是,我在任何地方都找不到下载包的链接。即使是这份文件: 谈到下载Windows Installer等,但没有说明从何处下载 我希望Python2.7的连接能够正常工作。从Python中获取PostgreSQL的最经典、文档最丰富的库可能是可以在windows上下载的。 如果您特别想要PyGreSQL,那么下载页面就是。从python中访问PostgreSQL的最经典、最有文档记录的

在Google上,我找到了关于PyGreSQL库的文档,这些文档可以帮助我将Python连接到Postgres

但是,我在任何地方都找不到下载包的链接。即使是这份文件:

谈到下载Windows Installer等,但没有说明从何处下载


我希望Python2.7的连接能够正常工作。从Python中获取PostgreSQL的最经典、文档最丰富的库可能是可以在windows上下载的。
如果您特别想要PyGreSQL,那么下载页面就是。

从python中访问PostgreSQL的最经典、最有文档记录的库可能就是可以为windows下载的库。
如果您特别想要PyGreSQL,下载页面是。

pypi上列出了不属于标准库的Python模块。 例如,pygresql模块列在以下页面中:

您还可以在页面中看到上次更新包的时间(在本例中为2013年),因此您可以选择使用python连接到postgresql,如psycopg2
pypi上列出了不属于标准库的Python模块。 例如,pygresql模块列在以下页面中:

您还可以在页面中看到上次更新包的时间(在本例中为2013年),因此您可以选择使用python连接到postgresql,如psycopg2
步骤1:pip安装psycopg2

步骤2:用户代码如下:-

import psycopg2

connection = psycopg2.connect(database="dbname", user="username", password="pass", host="hostname", port=5432)

cursor = connection.cursor()

cursor.execute("SELECT * from portal.portal_users;")

# Fetch all rows from database
record = cursor.fetchall()

print("Data from Database:- ", record)

步骤1:pip安装psycopg2

步骤2:用户代码如下:-

import psycopg2

connection = psycopg2.connect(database="dbname", user="username", password="pass", host="hostname", port=5432)

cursor = connection.cursor()

cursor.execute("SELECT * from portal.portal_users;")

# Fetch all rows from database
record = cursor.fetchall()

print("Data from Database:- ", record)

例如,如果在docker中使用PostreSQL

docker pull postgres

mkdir -p $HOME/docker/volumes/postgres

docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
850f9ee04731        postgres            "docker-entrypoint.s…"   44 hours ago        Up 44 hours         0.0.0.0:5432->5432/tcp   pg-docker
import psycopg2

connection = psycopg2.connect(database="postgres", user='postgres', password='docker', host="localhost", port=5432)

cursor = connection.cursor()

sql_context ="""
select 
    *
from 
    public.metrics sm
where 
    sm.metric_name not like '%test%'
group by 
    sm.metric_name
"""


cursor.execute(sql_context)

# Fetch all rows from database
record = cursor.fetchall()

print("Data from Database:- ", record)
您不需要安装psycopg2,对于此变体,您可以安装psycopg2二进制文件

pip install psycopg2-binary
之后可以使用例如

docker pull postgres

mkdir -p $HOME/docker/volumes/postgres

docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
850f9ee04731        postgres            "docker-entrypoint.s…"   44 hours ago        Up 44 hours         0.0.0.0:5432->5432/tcp   pg-docker
import psycopg2

connection = psycopg2.connect(database="postgres", user='postgres', password='docker', host="localhost", port=5432)

cursor = connection.cursor()

sql_context ="""
select 
    *
from 
    public.metrics sm
where 
    sm.metric_name not like '%test%'
group by 
    sm.metric_name
"""


cursor.execute(sql_context)

# Fetch all rows from database
record = cursor.fetchall()

print("Data from Database:- ", record)

例如,如果在docker中使用PostreSQL

docker pull postgres

mkdir -p $HOME/docker/volumes/postgres

docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
850f9ee04731        postgres            "docker-entrypoint.s…"   44 hours ago        Up 44 hours         0.0.0.0:5432->5432/tcp   pg-docker
import psycopg2

connection = psycopg2.connect(database="postgres", user='postgres', password='docker', host="localhost", port=5432)

cursor = connection.cursor()

sql_context ="""
select 
    *
from 
    public.metrics sm
where 
    sm.metric_name not like '%test%'
group by 
    sm.metric_name
"""


cursor.execute(sql_context)

# Fetch all rows from database
record = cursor.fetchall()

print("Data from Database:- ", record)
您不需要安装psycopg2,对于此变体,您可以安装psycopg2二进制文件

pip install psycopg2-binary
之后可以使用例如

docker pull postgres

mkdir -p $HOME/docker/volumes/postgres

docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
850f9ee04731        postgres            "docker-entrypoint.s…"   44 hours ago        Up 44 hours         0.0.0.0:5432->5432/tcp   pg-docker
import psycopg2

connection = psycopg2.connect(database="postgres", user='postgres', password='docker', host="localhost", port=5432)

cursor = connection.cursor()

sql_context ="""
select 
    *
from 
    public.metrics sm
where 
    sm.metric_name not like '%test%'
group by 
    sm.metric_name
"""


cursor.execute(sql_context)

# Fetch all rows from database
record = cursor.fetchall()

print("Data from Database:- ", record)

“不要问……任何与编写计算机程序没有直接关系的事情”这与编程没有直接关系。这是一个合理的问题。但这不是一个好问题。你想要的是psycopg2。前几次我也有同样的问题。我首先尝试了PyGreSQL,最后使用了我发现更好的psycopg2。这只是个人的观点,这就是为什么这样的问题不受欢迎的原因:很难给出好的论证和客观的答案。谢谢大家。我不喜欢PyGreSQL或psycopg2。任何能轻松完成工作的事情对我都有好处。“不要问……任何与编写计算机程序没有直接关系的事情。”这与编程没有直接关系。这是一个合理的问题。但这不是一个好问题。你想要的是psycopg2。前几次我也有同样的问题。我首先尝试了PyGreSQL,最后使用了我发现更好的psycopg2。这只是个人的观点,这就是为什么这样的问题不受欢迎的原因:很难给出好的论证和客观的答案。谢谢大家。我不喜欢PyGreSQL或psycopg2。任何能以最少的障碍完成工作的东西对我都有好处。