我正在尝试通过sqlalchemy在PostgreSQL中创建数据库。 当我尝试运行以下代码时:
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, ForeignKey, MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import sessionmaker
import psycopg2
engine = create_engine('postgresql://scott:tiger@localhost/postgres')
#connection = engine.connect()
base = declarative_base()
class schedule(base):
__tablename__ = 'schedule'
id = Column(Integer, primary_key=True)
time = Column(String)
monday = Column(String)
tuesday = Column(String)
wednesday = Column(String)
thursday = Column(String)
friday = Column(String)
saturday = Column(String)
base.metadata.create_all(engine)
我收到一个错误:psycopg2.OperationalError
错误回溯:
Traceback (most recent call last):
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\base.py", line 2275, in _wrap_pool_connect
return fn()
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 363, in connect
return _ConnectionFairy._checkout(self)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 639, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\default.py", line 481, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\psycopg2\__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\program files (x86)\microsoft visual studio\2019\enterprise\common7\ide\extensions\microsoft\python\core\ptvsd_launcher.py", line 119, in <module>
vspd.debug(filename, port_num, debug_id, debug_options, run_as)
File "c:\program files (x86)\microsoft visual studio\2019\enterprise\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\debugger.py", line 39, in debug
run()
File "c:\program files (x86)\microsoft visual studio\2019\enterprise\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "H:\prog_vs\chatBot\chatBot\chatBot\chatBot.py", line 23, in <module>
base.metadata.create_all(engine)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\sql\schema.py", line 4304, in create_all
ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\base.py", line 2045, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\base.py", line 2037, in _optional_conn_ctx_manager
with self._contextual_connect() as conn:
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\base.py", line 2239, in _contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\base.py", line 2279, in _wrap_pool_connect
e, dialect, self
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\base.py", line 1544, in _handle_dbapi_exception_noconnection
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\base.py", line 2275, in _wrap_pool_connect
return fn()
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 363, in connect
return _ConnectionFairy._checkout(self)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\pool\base.py", line 639, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\default.py", line 481, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\Nikola\AppData\Local\Programs\Python\Python37\lib\site-packages\psycopg2\__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)
(Background on this error at: http://sqlalche.me/e/e3q8)
Press any key to continue . . .
数据库看起来像这样
答案 0 :(得分:0)
@ilja-everilä 的评论回答了这个问题:
<块引用>连接字符串 'postgresql://scott:tiger@localhost/postgres' 是实际的连接字符串,还是这个问题的占位符?看起来这归结为无法连接。
数据库连接 URI 仍在使用对数据库无效的占位符凭据(用户“scott”,密码为“tiger”)。