没有选择要创建的模式...错误

No schema has been selected to create in ... error

我正在使用托管在亚马逊服务器上的 Linux 服务器,我已经完全设置了服务器。我要做的最后一件事是托管我在 Flask 框架中的服务器上创建的一个旧项目。

我正在尝试 运行 Python 文件,该文件设置了 运行 我的项目所需的数据库。

我在服务器内部使用一个虚拟机,它将 运行 我的项目,每次我 运行 命令我都会收到以下错误:

(venv) grader@ip-10-20-6-95:/var/www/catalog/catalog$ python setup_database.py
Traceback (most recent call last):
  File "setup_database.py", line 63, in <module>
    Base.metadata.create_all(engine)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/schema.py", line 2848, in create_all
    tables=tables)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1479, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1122, in _run_visitor
    **kwargs).traverse_single(element)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 122, in traverse_single
    return meth(obj, **kw)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/ddl.py", line 70, in visit_metadata
    self.traverse_single(table, create_ok=True)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 122, in traverse_single
    return meth(obj, **kw)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/ddl.py", line 89, in visit_table
    self.connection.execute(schema.CreateTable(table))
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 662, in execute
    params)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 720, in _execute_ddl
    compiled
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 874, in _execute_context
    context)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
    exc_info
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 196, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 867, in _execute_context
    context)
  File "/var/www/catalog/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 324, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (ProgrammingError) no schema has been selected to create in
 '\nCREATE TABLE users (\n\tid SERIAL NOT NULL, \n\tusername VARCHAR(100), \n\temail VARCHAR(225) NOT NULL, \n\tprofile_pic VARCHAR(225) NOT NULL, \n\tPRIMARY KEY (id)\n)\n\n' {}

我不确定为什么会出现此错误。
我 运行 设置 PostgreSQL 的命令(如果重要的话):

$ sudo apt-get install libpq-dev python-dev
$ sudo apt-get install postgresql postgresql-contrib
$ sudo su - postgres
$ psql
# CREATE USER catalog WITH PASSWORD 'sillypassword';
# ALTER USER catalog CREATEDB;
# CREATE DATABASE catalog WITH OWNER catalog;
# \c catalog
# REVOKE ALL ON SCHEMA public FROM public;
# GRANT ALL ON SCHEMA public TO catalog;
# \q
$ exit

我该如何解决这个问题?

no schema has been selected to create in

当您的 search_path 设置没有有效的第一个条目(通常为空)时,您会收到此错误。 Postgres 不知道在哪个模式中创建 table.

修复您的 search_path 设置或架构限定对象名称(如:public.users)。但是在 any 情况下修复你的 search_path
详情:

  • How does the search_path influence identifier resolution and the "current schema"

我发现 pg_dump 创建的文件(在 postgres 10.7 下)有

SELECT pg_catalog.set_config('search_path', '', false);

接近它的顶部。因此,在导入文件时,它操纵了搜索路径,该路径在当前会话中一直存在。

评论该行(并开始新会话)解决了问题。

此问题已得到解答:https://dba.stackexchange.com/a/275116/114247

修复方法是:

grant usage on schema public to public;
grant create on schema public to public;

您没有创建架构。使用 CREATE SCHEMA public; 创建架构 然后你可以执行 grant usage on schema public to public;grant create on schema public to public