running migration for multiple schema - typeorm/postgres - postgresql

I have a schema based multitenancy setup in postgres.
Typeorm allows us to run migration using a single command:
typeorm migration:run -d db.connection.js
the connection is defined like this:
{
type: 'postgres',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
ssl: process.env.DB_SSL === 'true',
schema,
migrations: migrations || ['./**/*.migration.js'],
...options,
}
Now, I have 3 schema in my database: A, B and C, how do I run the migration for all 3 of them at once? This is a really big issue for me as manually triggering migration for each schema individually is time consuming and cumbersome. Does typeorm have a method to specify multiple schemas?

Related

What is correct dbt profiles for postgresql?

I have a dbt_project like below.
name: 'DataTransformer'
version: '1.0.0'
config-version: 2
profile: 'DataTransformer'
Using Postgres, hence I have a profile at .dbt/profiles.yml
DataTransformer:
target: dev
outputs:
dev:
type: postgres
host: localhost
port: 55000
user: postgres
pass: postgrespw
dbname: postgres
schema: public
threads: 4
But when I run dbt debug, I got Credentials in profile "DataTransformer", target "dev" invalid: ['dbname'] is not of type 'string'
I have searched, there are several people has encountered the same error but in different databases. Still not sure why happens in my case

Spring Boot + PostgreSQL: cannot find schema to create Camunda tables

I am trying to create tables for camunda (7.14.0) at Spring Boot application start. I have manually created a PostgreSQL schema in advance with the name "camunda". When I run the Spring Boot application, it gives me an error:
ENGINE-03017 Could not perform operation 'create' on database schema for SQL Statement ...
and
org.postgresql.util.psqlexception: no schema has been selected to create in
My application.yml config:
camunda:
bpm:
database:
type: postgres
schema-update: create
schema-name: camunda
username: camunda
password: camunda
table-prefix: camunda.
How can I create camunda tables in a schema with a specific name?
Example config:
spring.datasource:
url: jdbc:postgresql://localhost:5432/postgres?autoReconnect=true
username: cam1
password: cam1
driver-class-name: org.postgresql.Driver
camunda:
bpm:
database:
schema-name: cam1
table-prefix: cam1.
Ensure the schema has been created and user used has been given the permissions on the target schema.
Full example for two Camunda instances using separate schemas: https://github.com/rob2universe/two-camunda-instances

Error creating a connection to Postgresql while preparing konga database

After installing Konga, we are trying to prepare Konga database on the already running Postgresql database. by using suggested command i.e.
node ./bin/konga.js prepare --adapter postgres --uri postgresql://localhost:5432/konga
But we are facing the error as below:
Error creating a connection to Postgresql using the following settings:
postgresql://localhost:5432/konga?host=localhost&port=5432&schema=true&ssl=false&adapter=sails-postgresql&user=postgres&password=XXXX&database=konga_database&identity=postgres
* * *
Complete error details:
error: password authentication failed for user "root"
error: A hook (`orm`) failed to load!
error: Failed to prepare database: error: password authentication failed for user "root"
We even created the schema konga_database manually and have tried several variations for prepare command but no fate
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong:XXXX#localhost:5432/konga_database
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong#localhost:5432/konga
node ./bin/konga.js prepare --adapter postgres --uri postgresql://kong#localhost:5432/konga_database
Below is config/connections.js
postgres: {
adapter: 'sails-postgresql',
url: process.env.DB_URI,
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || 'XXXX',
port: process.env.DB_PORT || 5432,
database: process.env.DB_DATABASE ||'konga_database',
// schema: process.env.DB_PG_SCHEMA ||'public',
// poolSize: process.env.DB_POOLSIZE || 10,
ssl: process.env.DB_SSL ? true : false // If set, assume it's true
},
Below is .env file configuration
PORT=1337
NODE_ENV=production
KONGA_HOOK_TIMEOUT=120000
DB_ADAPTER=postgres
DB_URI=postgresql://localhost:5432/konga
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=XXXX
KONGA_LOG_LEVEL=info
TOKEN_SECRET=
kong and postgresql are already running on the AWS linux AMI 2 server on there respective ports i.e. 8443 & 5432
Please help us to prepare DB and start konga service. Also. let us know in case you need more info.
Node v: v12.19.0
NPM v: 6.14.8
Regards
Nitin G
Maybe I overlooked it, but what version of PostreSQL are you using?
Konga is not able to support postgresql 12:
https://github.com/pantsel/konga/issues/487
Have you tried like this?
.env
DB_URI=postgresql://userdb:passworddb#localhost:5432/kongadb
I tried on Postgresql 9.6
https://www.rosehosting.com/blog/how-to-install-postgresql-9-6-on-ubuntu-20-04/

Sequelize migration tool can not find schema defined in config.json

When start a docker container from postgres image, it only creates a public schema under a database. here is my setting
postgres:
container_name: postgres
image: postgres:latest
ports:
- "${DB_PORT}:5432"
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
my solution is
// 111111-create-schema.js
await queryInterface.createSchema('custom');
// 222222-create-user.js
await queryInterface.createTable('Users', {}, {schema: 'custom'}
When I run npx sequelize-cli db:migrate, I do see my user table in custom schema. However, the sequelizeMeta table which stores the migrations is in PUBLIC shema
I also tried to add schema: 'custom' in config.json. however, I got custom schema is not found error when I run db:migrate. I assume at that point my migration script has not run yet before it trys to locate this custom schema.
Then I tried to manually create the schema in the pgadmin. then I see both my user and sequelizeMeta table are in this custom schema.
I am wondering how I can let postgres docker container precreate a schema just like it precreate a database using a POSTGRES_DB environment variable ? something like
postgres:
container_name: postgres
image: postgres:latest
ports:
- "${DB_PORT}:5432"
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
- POSTGRES_SCHEMA=${DB_SCHEMA} <---------------
Or there is an easy or proper way in sequelize migration that I missed ?
When you create your sequelize connection say your schema on define object like this:
const sequelize = new Sequelize(database, username, password,
{
host, dialect, port, omitNull: true,
define: {
schema: "your-schema"
timestamps: true,
underscored: true
},
...
})

what is client property of knexfile.js

In knex documentation of configuration of knexfile.js for PostgreSQL, they have a property called client, which looks this way:
...
client: 'pg'
...
However, going through some other projects that utilize PostgreSQL I noticed that they have a different value there, which looks this way:
...
client: 'postgresql'
...
Does this string correspond to the name of some sort of command line tool that is being used with the project or I misunderstand something?
Postgresql is based on a server-client model as described in 'Architectural Fundamentals'
psql is the standard cli client of postgres as mentioned here in the docs.
A client may as well be a GUI such as pg-admin, or a node-package such as 'pg' - here's a list.
The client parameter is required and determines which client adapter will be used with the library.
You should also read the docs of 'Server Setup and Operation'
To initialize the library you can do the following (in this case on localhost):
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
})
The standard user of the client deamon ist 'postgres' - which you can use of course, but its highly advisable to create a new user as stated in the docs and/or apply a password to the standard user 'postgres'.
On Debian stretch i.E.:
# su - postgres
$ psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'SecretPasswordHere';"
Make sure you delete the command line history so nobody can read out your pwd:
rm ~/.psql_history
Now you can add a new user (i.E. foobar) on the system and for postgres
# adduser foobar
and
# su - postgres
$ createuser --pwprompt --interactive foobar
Lets look at the following setup:
module.exports = {
development: {
client: 'xyz',
connection: { user: 'foobar', database: 'my_app' }
},
production: { client: 'abc', connection: process.env.DATABASE_URL }
};
This basically tells us the following:
In dev - use the client xyz to connect to postgresqls database my_app with the user foobar (in this case without pwd)
In prod - retrieve the globalenv the url of the db-server is set to and connect via the client abc
Here's an example how node's pg-client package opens a connection pool:
const pool = new Pool({
user: 'foobar',
host: 'someUrl',
database: 'someDataBaseName',
password: 'somePWD',
port: 5432,
})
If you could clarify or elaborate your setup or what you like to achieve a little more i could give you some more detailed info - but i hope that helped anyways..