Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x psycopg2.OperationalError:无法连接到服务器:连接被拒绝(0x0000274D/10061)_Python 3.x_Virtualbox_Vagrant Windows - Fatal编程技术网

Python 3.x psycopg2.OperationalError:无法连接到服务器:连接被拒绝(0x0000274D/10061)

Python 3.x psycopg2.OperationalError:无法连接到服务器:连接被拒绝(0x0000274D/10061),python-3.x,virtualbox,vagrant-windows,Python 3.x,Virtualbox,Vagrant Windows,我正在Windows 10上使用Virtualbox/Vagrant,正在学习Udacity完整堆栈开发人员课程。出于某种奇怪的原因,我无法通过python代码连接到数据库“news”。这是我目前掌握的代码: import psycopg2 DBNAME = "news" #What are th most popular three articles of all time? db = psycopg2.connect(dbname=DBNAME, port=5432) #conne

我正在Windows 10上使用Virtualbox/Vagrant,正在学习Udacity完整堆栈开发人员课程。出于某种奇怪的原因,我无法通过python代码连接到数据库“news”。这是我目前掌握的代码:

import psycopg2

DBNAME = "news"

#What are th most popular three articles of all time?


db = psycopg2.connect(dbname=DBNAME, port=5432)  #connects to database
c = db.cursor()
c.execute("SELECT * FROM articles")
pop_articles = c.fetchall()
pop_articles
db.close()
这是我不断收到的错误,让我发疯:

回溯(最近一次呼叫最后一次):

文件“C:\Users\tom\Downloads\fsnd virtual machine\fsnd virtual machine\vagrant\Trying\logs\u analysis1.py”,中第10行
db=psycopg2.connect(dbname=dbname,port=5432)#连接到数据库
文件“C:\Users\tom\AppData\Local\Programs\Python\Python37\lib\site packages\psycopg2\\uuuu init\uuu.py”,第130行,在connect中
连接=连接(dsn,连接工厂=连接工厂,**kwasync)
psycopg2.OperationalError:无法连接到服务器:连接被拒绝(0x0000274D/10061)
服务器是否在主机“localhost”(::1)上运行并接受
端口5432上的TCP/IP连接?
无法连接到服务器:连接被拒绝(0x0000274D/10061)
服务器是否在主机“localhost”(127.0.0.1)上运行并接受
端口5432上的TCP/IP连接?
[以2.4s完成]
奇怪的是,我可以通过gitbash访问数据库,而在vagrant目录中,但我无法让python代码返回任何东西,而不会出现此错误。我能想到的唯一一件事是,数据库不允许我的代码连接,因为它来自虚拟机之外(即使此文件保存在vagrant目录中),而且我不得不强制它在所有端口上侦听。不幸的是,我没有postgres的密码,因此我无法更改配置以允许此操作


任何提示或帮助都将不胜感激。我已经为此工作了好几天。

所以数据库在一个流浪的虚拟机中,Python脚本在真正的机器上。对的当您从虚拟机内访问数据库时,它是在
localhost:5432
还是
0.0.0.0:5432
(或其他)上?您是否已在端口5432上设置端口转发?(您可以在VirtualBox设置的“网络”下的某个地方看到这个,您可以在Vagrant中配置它,但我不知道如何配置。)
  File "C:\Users\tom\Downloads\fsnd-virtual-machine\FSND-Virtual-Machine\vagrant\Trying\logs_analysis1.py", line 10, in <module>
    db = psycopg2.connect(dbname=DBNAME, port=5432)  #connects to database
  File "C:\Users\tom\AppData\Local\Programs\Python\Python37\lib\site-packages\psycopg2\__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

[Finished in 2.4s]