create schema on created database on pg-app-dev-vm - postgresql

I've cloned this Github repository PostgreSQL VM via Vagrant. Been trying to create a new schema test but it always ends up under postgres database. Considering the create database is named myapp, how can I create the test schema under myapp database?
I've been trying to add this script under the Vagrant-setup/bootstrap.sh.
cat << EOF | su - postgres -c psql myapp
create schema test;
EOF

As a reference it may help to check your connection string in order to ensure you are connected to the right database. The official postgresql documentation is quite helpful here:
https://www.postgresql.org/docs/current/libpq-connect.html
This may help as well for understanding the format:
What is the format for the PostgreSQL connection string / URL?
Outside of that you will have to ensure permissions are set appropriately for the user to create schemas in the database and to configure the search_path for easy access to objects under the schema.

Related

Adonis and PostgreSQL: create table "adonis_schema"... fails with 'permission denied for schema public'

I have been following the dockerizing-adonis tutorial which uses PostgreSQL as database.
I am new to Postgres and do not completely understand its user/role/schema concepts yet.
In the tutorial pretty early on I'm instructed to create dockerConfig/postgres-dev-init.sql with this content:
CREATE USER adonis with encrypted password 'adonis';
CREATE DATABASE adonis_app;
GRANT ALL PRIVILEGES ON DATABASE adonis_app TO adonis;
Then further down in the docker-compse.yml there is
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
I followed the tutorial and also updated the .env file - the tutorial works - but does not use the database.
So I created a model with a migration: node ace make:model User -m
now I want to run that migration to create the User table.
Running node ace migration:run gets me the error
[ error ] create table "adonis_schema" ("id" serial primary key,
"name" varchar(255) not null, "batch" integer not null,
"migration_time" timestamptz default CURRENT_TIMESTAMP) - permission
denied for schema public
I want this setup with 2 users (postgres as the root user, adonis to be used by adonis) to work the way its suggested in the tutorial and understand postgre better (later i want to add a grafana user with just read-access).
Why is the permission denied? Does the init .sql file need more instructions? Shouldn't the user adonis by default already have the required create rights on the public schema, or am I misunderstanding that concept of the public schema in postgres?
Am I somehow completely on the wrong track, or should the tutorial somehow make this work and misses something (In which case I would create an issue for the adonis doc ...)?
Tutorial should work. I use it myself :)
I believe you skipped this part of docs:

prisma db pull doesn't see a new table

I have existing schema for prisma.
I copied the table from another schema to be included in the Prisma schema.
But when I run prisma db pull new table doesn't appear in Prisma schema.
Why?
If you use supabase and running this command and it returns something like this 'The following models were commented out because we couldn't retrieve columns for them. Please check your privileges.' or something similar regarding privileges, the solution is to go to your SQL editor from supabase and put this command and execute it
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO postgres;
You're misinterpreting the function of the prisma db pull command.
From the docs
The db pull command connects to your database and adds Prisma models to your Prisma schema that reflect the current database schema.
Basically, it will make your Prisma schema match the existing database schema. What you want is the opposite here: Update the database schema to match the current Prisma schema.
You can do this in two ways:
If you want to update the database and keep the changes in your migration history, use the prisma migrate dev command. More Info
If you just want to update the database without creating a migration, use the prisma db push command. More info
More information is available in the docs explaining how you should choose between 1 and 2.
I had a similar issue once and a quick check confirmed for me that it was the lack of security permissions granted for prisma on new table in the database itself.
Try this:
Note the name of the database user that Prisma connects to the database with. You'll likely find this via your schema.prisma file, or perhaps via a DATABASE_URL config setting in the related .env file if you're using that with prisma.
Go into the database itself and ensure that database user which Prisma connects with has been granted sufficient security privileges to that new table. (note: what 'sufficient' is I cannot say since it depends on your own needs. At a guess, I'd say at least 'select' permission would be needed.)
Once you've ensure that user has sufficient privileges, try running a prisma db pull command once again.
For reference, another thing you could do is:
cross-check against one of the other tables that is already in your database that works correctly with prisma.
compare the security privileges of that old table with the security privileges of the new table and see if there are any differences.

Postgres for Airflow backend DB to create metadata tables in a specified schema

I'm setting up postgres backend for Airflow metadata DB using the following sqlalchemy connection string:
sql_alchemy_conn = postgresql+psycopg2://<user>:<pass>#<host>:5432/<db>
When I run initdb/upgradedb, the tables are created in public schema.
Is there a way to create the tables in a specified schema?
Can the schema where I would like the tables to be created be specified as part of the connection string?
From the docs:
Also note that since SqlAlchemy does not expose a way to target a specific schema in the Postgres connection URI, you may want to set a default schema for your role with a command similar to ALTER ROLE username SET search_path = airflow, foobar;
So, connect to postgres, and set the search path for your user.

Can't create new schema in an OVH postgres

I have a PostgreSQL server on OVH's Cloud DB and have been using its databases for my web apps.
So far so good.
I got a project where It's a requirement to have schemas. Strangely enough, I am unable to create schemas on the user with "Administrator" privileges.
I have prepared scripts using schemas, so I just need to run them on a prepared database but I need a database with schemas to run them.
Here is my process:
Create a new database
Select option "Create user"
Select option for privilages: "Administrator"
Commit configuration
Wait for database creation
Connect to database with the new config via PGAdmin
Run command create schema if not exists "vMobile";
Recieve following error:
ERROR: permission denied for database my-database-dev
SQL state: 42501
I created a ticket for this but the wait is taking too long.
Support answer
Ok, so I got a response from the OVH support and there is no option for the user to create new schemas as their CloudDB enables access only to schema public and mentioned privileges Administrator, Read/Write, Read, None are only applicable to the public schema.
Workaround
My solution to this is to create tables with schema name included in their names
like so:
Desired outcome: "vCommon"."Route"
Workaround: "public"."vCommon_Route"

Why can I only see postgreSQL relations or tables when I'm logged in as the postgres user? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
postgresql database owner can't access database - “No relations found.”
The core problem: I have a Django website that's complaining about relations (tables) not existing when I connect to it after attempting to run a SQL script generated by pg_dump MY_DATABASE_NAME -cOx -E UTF8 > MY_SCRIPT_NAME.sql
Before running the script I'd could connect and all was right with the world.
After running the script I live in a world of pain that I've attempted to alleviate by examining and altering ownership of MY_DATABASE_NAME and tables using the various methods described in Modify OWNER on all tables simultaneously in PostgreSQL
It would seem that I can only see relations via \dt on MY_DATABASE_NAME only after having first logged in as the postgres user on my server like so: sudo su - postgres.
When I SSH and run psql MY_DATABASE_NAME -U MY_NON_POSTGRES_USER without switching to the postgres user, \dt' results in ano relations found` message.
At first I presumed permissions or ownership was the problem. I logged-in as my server's postgres user and changed owner of MY_DATABASE_NAME and all of its tables to MY_NON_POSTGRES_USER.
Same results as before. Relations show up for my postgres user, but logging in as another role results in a 'no relations found message', and my website complains that 'relation some_table doesn't exist'
Why is this happening to me?
Wow. I really just composed a TL;DR description of my question and realized immediately afterwards my folly:
Although I'd connected to MY_DATABASE_NAME and given ownership to the correct user for all tables and the database, I'd forgotten to:
REVOKE ALL ON SCHEMA public FROM MY_NON_POSTGRES_USER;
GRANT ALL ON SCHEMA public TO MY_NON_POSTGRES_USER;
No more pain. All is right with the world again.