Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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 psycopg2.OperationalError:致命:数据库“新闻”不存在_Python_Postgresql_Vagrant - Fatal编程技术网

Python psycopg2.OperationalError:致命:数据库“新闻”不存在

Python psycopg2.OperationalError:致命:数据库“新闻”不存在,python,postgresql,vagrant,Python,Postgresql,Vagrant,正在尝试设置到我的postgres sql数据库的连接,以便通过python代码运行查询 我在MAC mojave 10.14.6上,正试图通过python脚本连接到我的virtal postgres数据库。我可以通过终端窗口访问数据库,但当我尝试使用psycopg2连接时,我会得到psycopg2。操作错误:致命:数据库学生不存在 我检查了我只有一个postgres实例,我检查了正在使用的端口,并将其放入参数中 Postgres版本是11.5 Python版本是Python 3.7.4 我已经

正在尝试设置到我的postgres sql数据库的连接,以便通过python代码运行查询

我在MAC mojave 10.14.6上,正试图通过python脚本连接到我的virtal postgres数据库。我可以通过终端窗口访问数据库,但当我尝试使用psycopg2连接时,我会得到psycopg2。操作错误:致命:数据库学生不存在

我检查了我只有一个postgres实例,我检查了正在使用的端口,并将其放入参数中

Postgres版本是11.5 Python版本是Python 3.7.4 我已经检查了psql/usr/local/bin/psql的位置 我已经检查了postgres/usr/local/bin/postgres的位置

在任何其他职位上推荐的东西似乎都不管用

import psycopg2

#DBNAME = 'students'

def connect():

    print('Trying to connect to server ...')
    conn = psycopg2.connect(dbname = 'students')
    cur = conn.cursor()

    cur.execute('select * from students')


    results = cur.fetchall()
    print(results)

    cur.close
    conn.close

if __name__ == '__main__':
    connect()

---------------------------------------------------------------------

Below works when I connect to the postgres database
-----------------------------------------------------------------------
import psycopg2

def connect():

    print('Trying to connect to server ...')

    conn = psycopg2.connect(dbname = 'postgres')
    cur = conn.cursor()

    print('PostgreSQL database version:')
    cur.execute('select version()')
    db_version = cur.fetchone()
    print(db_version)


    cur.close
    conn.close

if __name__ == '__main__':
    connect()

result:

RESTART: /Users/Karen/Documents/Backup/Karens_Documents/Web_Development/web_dev_nano/course-ud303/SQL/fullstack-nanodegree-vm-master/vagrant/db_test.py 
Trying to connect to server ...
PostgreSQL database version:
('PostgreSQL 11.5 on x86_64-apple-darwin18.6.0, compiled by Apple LLVM version 10.0.1 (clang-1001.0.46.4), 64-bit',)

Error:

Traceback (most recent call last):
  File "/Users/Karen/Documents/Backup/Karens_Documents/Web_Development/web_dev_nano/course-ud303/SQL/fullstack-nanodegree-vm-master/vagrant/db_test.py", line 28, in <module>
    connect()
  File "/Users/Karen/Documents/Backup/Karens_Documents/Web_Development/web_dev_nano/course-ud303/SQL/fullstack-nanodegree-vm-master/vagrant/db_test.py", line 9, in connect
    conn = psycopg2.connect(dbname = DBNAME)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  database "students" does not exist

你真的确定你的数据库学生确实存在吗?您使用什么命令连接终端中的数据库?psql学生:好的,看起来不错,我想除了学生之外,我还有多个数据库。我可以如上所述访问所有数据库,但不使用psycopg2.connect登录后,能否在代码的第二个版本中列出一次数据库?如何运行脚本?您是否也与vagrant user一起运行正如从CLI运行时一样,这可能是与用户相关的问题