Db create all ошибка postgresql

This is probably not the main reason why the create_all() method call doesn’t work for people, but for me, the cobbled together instructions from various tutorials have it such that I was creating my db in a request context, meaning I have something like:

# lib/db.py
from flask import g, current_app
from flask_sqlalchemy import SQLAlchemy

def get_db():
  if 'db' not in g:
    g.db = SQLAlchemy(current_app)
  return g.db

I also have a separate cli command that also does the create_all:

# tasks/db.py
from lib.db import get_db

@current_app.cli.command('init-db')
def init_db():
  db = get_db()
  db.create_all()

I also am using a application factory.

When the cli command is run, a new app context is used, which means a new db is used. Furthermore, in this world, an import model in the init_db method does not do anything, because it may be that your model file was already loaded(and associated with a separate db).

The fix that I came around to was to make sure that the db was a single global reference:

# lib/db.py
from flask import g, current_app
from flask_sqlalchemy import SQLAlchemy

db = None
def get_db():
  global db
  if not db:
    db = SQLAlchemy(current_app)
  return db

I have not dug deep enough into flask, sqlalchemy, or flask-sqlalchemy to understand if this means that requests to the db from multiple threads are safe, but if you’re reading this you’re likely stuck in the baby stages of understanding these concepts too.

This is probably not the main reason why the create_all() method call doesn’t work for people, but for me, the cobbled together instructions from various tutorials have it such that I was creating my db in a request context, meaning I have something like:

# lib/db.py
from flask import g, current_app
from flask_sqlalchemy import SQLAlchemy

def get_db():
  if 'db' not in g:
    g.db = SQLAlchemy(current_app)
  return g.db

I also have a separate cli command that also does the create_all:

# tasks/db.py
from lib.db import get_db

@current_app.cli.command('init-db')
def init_db():
  db = get_db()
  db.create_all()

I also am using a application factory.

When the cli command is run, a new app context is used, which means a new db is used. Furthermore, in this world, an import model in the init_db method does not do anything, because it may be that your model file was already loaded(and associated with a separate db).

The fix that I came around to was to make sure that the db was a single global reference:

# lib/db.py
from flask import g, current_app
from flask_sqlalchemy import SQLAlchemy

db = None
def get_db():
  global db
  if not db:
    db = SQLAlchemy(current_app)
  return db

I have not dug deep enough into flask, sqlalchemy, or flask-sqlalchemy to understand if this means that requests to the db from multiple threads are safe, but if you’re reading this you’re likely stuck in the baby stages of understanding these concepts too.

import sys
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()

class Restaurant(Base):
# Create Restaurant Table name#
tablename = ‘restaurant’
# Inside Restaurant Mappers are Created#
name = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)

class MenuItem(Base):
# Create Menu Item Table name#
tablename = ‘menu_item’
# Inside Menu Item Mappers are Created#
name = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
course = Column(String(250))
description = Column(String(250))
price = Column(String(8))
restaurant_id = Column(Integer, ForeignKey(‘Restaurant.id’))
restaurant = relationship(Restaurant)

####Insert at the end of the file#####

Create an engine that stores data in the local directory’s

restaurantmenu.db file.

engine = create_engine(‘sqlite :///restaurantmenu.db’)

Create all tables in the engine. This is equivalent to «Create Table»

statements in raw SQL.

metadata = MetaData(engine)
Base.metadata.create_all(engine)

gets an error

$ python database_setup.py
Traceback (most recent call last):
File «database_setup.py», line 35, in
engine = create_engine(‘sqlite :///restaurantmenu.db’)
File «C:Python35libsite-packagessqlalchemy-1.1.5-py3.5-win-amd64.eggsqlalchemyengine_init_.py», line 387, in create_engine
File «C:Python35libsite-packagessqlalchemy-1.1.5-py3.5-win-amd64.eggsqlalchemyenginestrategies.py», line 50, in create
File «C:Python35libsite-packagessqlalchemy-1.1.5-py3.5-win-amd64.eggsqlalchemyengineurl.py», line 194, in make_url
File «C:Python35libsite-packagessqlalchemy-1.1.5-py3.5-win-amd64.eggsqlalchemyengineurl.py», line 243, in _parse_rfc1738_args
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string ‘sqlite :///restaurantmenu.db’

Перейти к контенту

The first comment nailed the most likely reason this is happening. Quoting the release announcement:

PostgreSQL 15 also revokes the CREATE permission from all users except a database owner from the public (or default) schema.

The reason your fix didn’t work is that all actions you took on database postgres in regards to user admin‘s privileges on schema public concern only that schema within the database postgres. Schema public on database postgres is not the same schema public as the one on newly created mydb.

Also, this:

GRANT ALL ON DATABASE mydb TO admin;

grants privileges on the database itself, not things within the database. admin can now drop the database, for example, still without being able to create tables in schema public. My guess is that you wanted to make admin also the owner of mydb, in which case you need to add

ALTER DATABASE mydb OWNER TO admin;

Or you need to repeat your GRANT USAGE, CREATE ON SCHEMA public TO admin; on mydb.

Here’s some more documentation on secure schema usage patterns the PostgreSQL 15 change was based on.

I kept getting this error when using flyway to deploy database changes. I do some manual setup first, such as creating the database, so flyway wouldn’t need those super-admin permissions.

My Fix

I had to ensure that the database user that flyway job used had ownership rights to the public schema, so that the flyway user could then assign the right to use the schema to other roles.

Additional setup Details

I am using AWS RDS (both regular and Aurora), and they don’t allow super users in the databases. RDS reserves super users for use by AWS, only, so that consumers are unable to break the replication stuff that is built in. However, there’s a catch-22 that you must be an owner in postgres to be able to modify it.

My solution was to create a role that acts as the owner (‘owner role’), and then assign both my admin user and the flyway user to the owner role, and use ALTER scripts for each object to assign the object’s owner to the owner role.

I missed the public schema, since that was auto-created when I created the database script manually. The public schema defaulted to my admin role rather than the shared owner role. So when the flyway user tried to assign public schema permissions to other roles, it didn’t have the authority to do that. An error was not thrown during flyway execution, however.

I kept getting this error when using flyway to deploy database changes. I do some manual setup first, such as creating the database, so flyway wouldn’t need those super-admin permissions.

My Fix

I had to ensure that the database user that flyway job used had ownership rights to the public schema, so that the flyway user could then assign the right to use the schema to other roles.

Additional setup Details

I am using AWS RDS (both regular and Aurora), and they don’t allow super users in the databases. RDS reserves super users for use by AWS, only, so that consumers are unable to break the replication stuff that is built in. However, there’s a catch-22 that you must be an owner in postgres to be able to modify it.

My solution was to create a role that acts as the owner (‘owner role’), and then assign both my admin user and the flyway user to the owner role, and use ALTER scripts for each object to assign the object’s owner to the owner role.

I missed the public schema, since that was auto-created when I created the database script manually. The public schema defaulted to my admin role rather than the shared owner role. So when the flyway user tried to assign public schema permissions to other roles, it didn’t have the authority to do that. An error was not thrown during flyway execution, however.

In Postgres I created the following table inside a db called testing:

CREATE TABLE category_google_taxonomy (
    category_id integer references category ON UPDATE CASCADE ON DELETE CASCADE,
    google_taxonomy_id integer references google_taxonomy ON UPDATE CASCADE ON DELETE     CASCADE
);

When I try to populate the table:

INSERT INTO category_google_taxonomy (category_id, google_taxonomy_id) VALUES
(1,7),
(2,12);

I get the following error:

ERROR:  permission denied for schema public
LINE 1: SELECT 1 FROM ONLY "public"."category" x WHERE "category_id"...
                       ^
QUERY:  SELECT 1 FROM ONLY "public"."category" x WHERE "category_id" OPERATOR(pg_catalog.=) $1 FOR SHARE OF x

I read up a bit and eventually granted ALL PRIVILEGES out of exasperation, but it still doesn’t work:

testing=# GRANT ALL PRIVILEGES ON public.category TO testing;
GRANT

testing=# dp category_google_taxonomy
                                   Access privileges
 Schema |           Name           | Type  |    Access privileges    | Column access privileges 
--------+--------------------------+-------+-------------------------+--------------------------
 public | category_google_taxonomy | table | testing=arwdDxt/testing | 
                                           : super=arwdDxt/testing 


testing=# dp category
                           Access privileges
 Schema |   Name   | Type  |   Access privileges    | Column access privileges 
--------+----------+-------+------------------------+--------------------------
 public | category | table | testing=arwdDxt/super | category_id:
                                                :   testing=arwx/super
(1 row)

On @Daniel’s suggestion I tried GRANT USAGE ON schema public TO super;, now when I run the INSERT command I get:

ERROR:  permission denied for relation category
CONTEXT:  SQL statement "SELECT 1 FROM ONLY "public"."category" x WHERE "category_id" OPERATOR(pg_catalog.=) $1 FOR SHARE OF x"

Here is the relevant part of d:

public | category                               | table    | super
public | category_google_taxonomy               | table    | testing

Я запускаю Postgres 10.4 и в настоящее время сбит с толку, поскольку не могу предоставить доступ к схеме другой роли.

Что я хочу сделать:

У меня одна роль с одной схемой, и я хочу получить доступ к схеме и ее таблицам из другой роли. Итак, я сделал как обычно (что работало с другими схемами):

grant usage on schema myschema to newuser;

grant select on all tables in schema myschema to newuser;

Оба этих оператора выполнялись как владелец схемы. При этом я не столкнулся с какими-либо ошибками.

Когда я вхожу в систему как новый пользователь и пытаюсь выбрать некоторые данные:

select * from myschema.table;

Я получаю сообщение об ошибке:

SQL Error [42501]: ERROR: permission denied for schema myschema

Я вижу, что у нового пользователя есть нужные привилегии в таблице «information_schema.role_table_grants»

Он также работал с другой ролью и другой схемой. Я невежественен.


Ответы
2

Это определенно работает, как указано в моем вопросе, проблема заключалась в том, что я не использовал владельца схемы.

Поэтому всегда убедитесь, что вы предоставляете доступ к схеме от роли владельца.

Шаг 1
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA name_schema TO name_user;

Шаг 2
GRANT USAGE ON SCHEMA name_schema TO name_user;

Другие вопросы по теме

NataNov

0 / 0 / 0

Регистрация: 09.04.2021

Сообщений: 4

1

Создание пользователя с правами на таблицы в схеме

09.04.2021, 14:54. Показов 4679. Ответов 1

Метки нет (Все метки)


Добрый день!
Изучаю PostgreSql и не могу разобраться в следующем.
Хочу создать пользователя с правами выбора из всех таблиц некоторой схемы в БД.
Все дальнейшие действия делаю в утилите psql под суперпользователем postgres:

SQL
1
2
3
4
5
6
7
8
CREATE DATABASE my_db;
c my_db
CREATE schema my_schema;
SET search_path TO my_schema,public;
CREATE TABLE my_table (f1 int2,f2 text);
INSERT INTO my_table VALUES (1,'aaa');
CREATE ROLE user1 login password 'user1';
GRANT SELECT ON ALL TABLES IN schema my_schema TO user1;

Затем подключаюсь к базе my_db под пользователем user1:

c my_db user1

До сих пор все шло прекрасно. Но дальше пытаюсь сделать select от имени пользователя user1:

SQL
1
SELECT * FROM my_schema.my_table;

выходит ошибка — нет доступа к схеме my_schema

пытаюсь по другому:

SQL
1
2
SET search_path TO my_schema,public;
SELECT * FROM my_table;

Ошибка — нет доступа к таблице my_table!

Что я делаю не так?!

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь

0

grgdvo

1184 / 914 / 367

Регистрация: 02.09.2012

Сообщений: 2,785

10.04.2021, 09:09

2

Лучший ответ Сообщение было отмечено NataNov как решение

Решение

Схема — такой же объект базы, как и таблицы.
Вы дали доступ на все таблицы в схеме, но не дали доступ к самой схеме.
См. что-то типа

SQL
1
GRANT USAGE ON SCHEMA my_schema TO user1;

1

Допустим, я сделал эту «простую» реализацию БД в Postgres.

postgres=# CREATE ROLE my_role;
           CREATE DATABASE my_db;
           GRANT ALL ON DATABASE my_db TO my_role;
           CREATE SCHEMA my_schm AUTHORIZATION my_role;

А потом я хочу сделать таблицу:

postgres=#CREATE TABLE IF NOT EXIST my_db.my_schm.table(...);

И получил следующую ошибку: cross-database references are not implemented: "my_db.my_schm.table"

После этого я попытался создать таблицу, подключенную к базе данных (т.е. c my_db), и получил следующую ошибку:

schema "my_schm"does not exist

Итак, я не понимаю поведения этих ошибок. Предполагается, что роль имеет все разрешения для базы данных (и да, я также пробовал использовать SET ROLE my_role;), но когда я прошу показать схемы в my_db действительно my_schm не существует, но в Postgres он есть. Может кто-нибудь объяснить мне, пожалуйста, почему это происходит? А также как сгруппировать таблицы в my_schm?

1 ответ

Лучший ответ

Схемы существуют только в одной базе данных и создаются в текущей базе данных: вы создали свою схему в базе данных postgres, а не в mydb.

Вам необходимо сначала подключиться к базе данных mydb, чтобы создать схему в базе данных mydb.


3

pifor
24 Май 2020 в 10:56

    msm.ru

    Нравится ресурс?

    Помоги проекту!

    !
    информация о разделе

    user posted image Данный раздел предназначается исключительно для обсуждения вопросов использования языка запросов SQL. Обсуждение общих вопросов, связанных с тематикой баз данных — обсуждаем в разделе «Базы данных: общие вопросы». Убедительная просьба — соблюдать «Правила форума» и не пренебрегать «Правильным оформлением своих тем». Прежде, чем создавать тему, имеет смысл заглянуть в раздел «Базы данных: FAQ», возможно там уже есть ответ.

    >
    разные пользователи и доступ
    , не могу разобраться с ролями

    • Подписаться на тему
    • Сообщить другу
    • Скачать/распечатать тему

      


    Сообщ.
    #1

    ,
    13.10.15, 19:17

      Senior Member

      ****

      Рейтинг (т): 13

      Здравствуйте, Господа!
      Сразу предупрежу: в PostgreSQL я пока еще совсем новичок.
      Опишу проблему на примере, думаю так станет понятнее.

      ExpandedWrap disabled

        #psql

        postgres=# CREATE USER user1 WITH password ‘password’;

        postgres=# CREATE DATABASE db1;

        postgres=# GRANT ALL PRIVILEGES ON DATABASE db1 TO user1;

        postgres=# CREATE USER user2 WITH password ‘password’;

        postgres=# GRANT ALL PRIVILEGES ON DATABASE db1 TO user2;

        postgres=# q

        #psql -U user1 db1

        db1=# CREATE SEQUENCE user_ids;

        db1=# CREATE TABLE users (id INTEGER PRIMARY KEY DEFAULT NEXTVAL(‘user_ids’), login CHAR(64), password CHAR(64));

        db1=# INSERT INTO users (login, password) VALUES («u1», «p1»);

        db1=# q

        #psql -U user2 db1

        db1=# INSERT INTO users (login, password) VALUES («u2», «p2»);

        ОШИБКА:  нет доступа к отношению users

      Вот тут я в растерянности. Оба пользователя ALL PRIVILEGES для db1, но второй, т.е. не создатель таблицы, добавлять записи не может.
      Как это побороть?


      grgdvo



      Сообщ.
      #2

      ,
      14.10.15, 12:36

        Member

        **

        Рейтинг (т): 21

        Опция ALL PRIVILEGES для DATABASE подразумевает CREATE, CONNECT и кажется TEMP привилегии для базы данных.
        Так вот CREATE позволяет создавать ТОЛЬКО схемы в рамках базы данных.
        А для схемы вы не предоставили прав, соответственно получили ошибку доступа.


        HighMan



        Сообщ.
        #3

        ,
        14.10.15, 14:57

          Senior Member

          ****

          Рейтинг (т): 13

          Цитата grgdvo @ 14.10.15, 12:36

          Опция ALL PRIVILEGES для DATABASE подразумевает CREATE, CONNECT и кажется TEMP привилегии для базы данных.
          Так вот CREATE позволяет создавать ТОЛЬКО схемы в рамках базы данных.
          А для схемы вы не предоставили прав, соответственно получили ошибку доступа.

          Простите бестолкового, а как для схем предоставить привилегии?
          Если, возможно, напишите запрос на основе моего примера.
          Спасибо!

          Сообщение отредактировано: HighMan — 14.10.15, 14:58


          grgdvo



          Сообщ.
          #4

          ,
          14.10.15, 20:28

            Member

            **

            Рейтинг (т): 21

            Цитата HighMan @ 14.10.15, 14:57

            Простите бестолкового, а как для схем предоставить привилегии?

            В конце я неправ, забываешь как оно работает, когда не пользуешься.
            Для схемы (как объекта) тоже будет недостаточно прав :(

            Нужны команды конкретно на таблицу или ALL TABLES IN SCHEMA.
            И права нужно раздавать после создания таблицы, либо умудриться использовать ALTER DEFAULT PRIVILEGES.

            Для вашего примера скорее всего правильный порядок будет такой

            ExpandedWrap disabled

              ~ # psql -U postgres

              postgres=# CREATE USER user1 WITH password ‘password’;

              postgres=# CREATE USER user2 WITH password ‘password’;

              postgres=# CREATE DATABASE db1;

              postgres=# q

            db1 создается со схемой public по умолчанию, в которой пользователи уже могут создавать таблицы (и т.д.), поэтому user1 и user2 смогут создать свои объекты базы

            ExpandedWrap disabled

              ~ # psql -U user1 db1

              db1=# CREATE SEQUENCE user_ids;

              db1=# CREATE TABLE users (id INTEGER PRIMARY KEY DEFAULT NEXTVAL(‘user_ids’), login CHAR(64), password CHAR(64));

              db1=# INSERT INTO users (login, password) VALUES (‘u1’, ‘p1’);

              db1=# q

            теперь к user_ids и users доступ имеет только user1, ибо он владелец этих объектов.
            Назначаем права user2

            ExpandedWrap disabled

              ~ # psql -U postgres db1

              db1=# GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO user2;

              db1=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO user2;

            Теперь user2 имеет все привилегии на таблицы и последовательнсти, которые УЖЕ БЫЛИ СОЗДАНЫ на данный момент в схеме.

            ExpandedWrap disabled

              ~ # psql -U user2 db1

              db1=# INSERT INTO users (login, password) VALUES (‘u2’, ‘p2’);

            Сообщение отредактировано: grgdvo — 14.10.15, 20:28


            HighMan



            Сообщ.
            #5

            ,
            14.10.15, 20:30

              Senior Member

              ****

              Рейтинг (т): 13

              Цитата grgdvo @ 14.10.15, 20:28

              Цитата HighMan @ 14.10.15, 14:57

              Простите бестолкового, а как для схем предоставить привилегии?

              В конце я неправ, забываешь как оно работает, когда не пользуешься.
              Для схемы (как объекта) тоже будет недостаточно прав :(

              Нужны команды конкретно на таблицу или ALL TABLES IN SCHEMA.
              И права нужно раздавать после создания таблицы, либо умудриться использовать ALTER DEFAULT PRIVILEGES.

              Для вашего примера скорее всего правильный порядок будет такой

              ExpandedWrap disabled

                ~ # psql -U postgres

                postgres=# CREATE USER user1 WITH password ‘password’;

                postgres=# CREATE USER user2 WITH password ‘password’;

                postgres=# CREATE DATABASE db1;

                postgres=# q

              db1 создается со схемой public по умолчанию, в которой пользователи уже могут создавать таблицы (и т.д.), поэтому user1 и user2 смогут создать свои объекты базы

              ExpandedWrap disabled

                ~ # psql -U user1 db1

                db1=# CREATE SEQUENCE user_ids;

                db1=# CREATE TABLE users (id INTEGER PRIMARY KEY DEFAULT NEXTVAL(‘user_ids’), login CHAR(64), password CHAR(64));

                db1=# INSERT INTO users (login, password) VALUES (‘u1’, ‘p1’);

                db1=# q

              теперь к user_ids и users доступ имеет только user1, ибо он владелец этих объектов.
              Назначаем права user2

              ExpandedWrap disabled

                ~ # psql -U postgres db1

                db1=# GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO user2;

                db1=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO user2;

              Теперь user2 имеет все привилегии на таблицы и последовательнсти, которые УЖЕ БЫЛИ СОЗДАНЫ на данный момент в схеме.

              ExpandedWrap disabled

                ~ # psql -U user2 db1

                db1=# INSERT INTO users (login, password) VALUES (‘u2’, ‘p2’);

              Спасибо большое!
              Постараюсь завтра проверить!

              0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)

              0 пользователей:

              • Предыдущая тема
              • Базы данных: SQL
              • Следующая тема

              Рейтинг@Mail.ru

              [ Script execution time: 0,0347 ]   [ 15 queries used ]   [ Generated: 30.01.23, 08:58 GMT ]  

              I am using ubuntu 18.04 and running postgres sql in a docker container running on port 5432 on a VM powered by GCP.

              First of all I entered sudo -u postgres psql to enter into the database as postgres superuser.

              After that executed the following commands as superuser

              CREATE DATABASE retail_db;
              CREATE USER retail_user WITH ENCRYPTED PASSWORD 'retail_password';
              

              Also granted the permission to the user retail_user on the db retail_db

              GRANT ALL ON DATABASE retail_db TO retail_user;
              

              All of this things im doing in my home directory where there’s a folder I have kept named retail_db which contains two SQL scripts one for table creation and one for data insertion into the table.
              After that to create the tables used the script to enter into the db

              psql -U retail_user -h localhost -p 5432 -d retail_db -W
              

              And to create the tables used

              i retail_db/create_db_tables_pg.sql
              

              Ideally it should create all the tables but im getting an error like

              psql:retail_db/create_db_tables_pg.sql:12: ERROR: permission denied for schema public
              LINE 1: CREATE TABLE departments (
              ^

              psql:retail_db/create_db_tables_pg.sql:23: ERROR: permission denied for schema public
              LINE 1: CREATE TABLE categories (
              ^

              psql:retail_db/create_db_tables_pg.sql:37: ERROR: permission denied for schema public
              LINE 1: CREATE TABLE products (
              ^

              psql:retail_db/create_db_tables_pg.sql:54: ERROR: permission denied for schema public
              LINE 1: CREATE TABLE customers (
              ^

              psql:retail_db/create_db_tables_pg.sql:66: ERROR: permission denied for schema public
              LINE 1: CREATE TABLE orders (
              ^

              psql:retail_db/create_db_tables_pg.sql:80: ERROR: permission denied for schema public
              LINE 1: CREATE TABLE order_items (

              I don’t understand here I have given all the permission to the retail_user but its still throwing this error.

              When i see the users table using du command inside the db console i get something like

                                                                     List of roles
                     Role name       |                         Attributes                         |       Member of        | Description 
              -----------------------+------------------------------------------------------------+------------------------+-------------
               demo_user             |                                                            | {}                     | 
               retail_user           |                                                            | {}                     | 
               sms1_user             |                                                            | {pg_read_server_files} | 
               postgres              | Superuser, Create role, Create DB, Replication, Bypass RLS | {}                     | 
               roster_user           |                                                            | {}                     | 
               sms_user              |                                                            | {}                     | 
              
              
              
              

              Is there anything im overdoing or missing?
              Am fairly new to Psql so don’t have much idea so apologies in advance.

            • Dayz ошибка память не может быть read
            • Dayz ошибка входа время авторизации истекло
            • Dayz ошибка unable to locate a running instance of steam на пиратке
            • Dayz ошибка gpu not supported
            • Dc 242 ошибка 010 320