Adding a database in a new cluster created on postgresql? - postgresql

I have created a database cluster in PostgreSQL-9.6 using pg_createcluster command with user (-u) specified different from default postgres. I would like to know how to add a new/existing database into this cluster instead of the default 'main' cluster?

Conect to the template1 or postgres database in the new cluster and execute sql
CREATE DATABASE etc

Related

How to set default database name when creating a cluster

When creating a cluster using initdb
$ initdb -D /usr/local/pgsql/data
it creates a default database user postgres and database name postgres.
I need to create a cluster with a different superuser name and default database name. I found the way to have a different name for superuser:
$ initdb --username=myuser
But can't find how to define the different name for the database instead of postgres. How can I do that? Is my only option is to rename the default DB after it's been created?
Creating the database "postgres" is hardcoded in the initdb-code. You could build your own version of initdb and use a different name for this specific database or you run a script afterwards that renames this database.

Cloud sql postgres DB create Cross project replication

In the Gcloud SQL Postgres database, I am currently using gcloud export and import command to create database backup in a different project. which takes a long time.
As gcloud allows us to create read replica but it creates with in the same project.
my question is?
how can i create a replica of cloud sql postgres instance in another project postgres instacne?

Apache Airflow - installation issue : sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidSchemaName) no schema has been selected to create in

I'm trying to install Airflow on Ubuntu 20.04 virtual machine with PostgreSQL DB which I have done previously without any problems.
I'm carefully following Airflow installation, in DB setup part:
https://airflow.apache.org/docs/apache-airflow/stable/howto/set-up-database.html
I ran into issues after trying to initialize db with:
airflow db init
gave me:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidSchemaName)
no schema has been selected to create in
It says:
Also note that since SqlAlchemy does not expose a way to target a
specific schema in the database URI, you may want to set a default
schema for your role with a SQL statement similar to
ALTER ROLE username SET search_path = airflow, foobar;
I'm assuming I have to use this since tutorial doesn't state which schema airflow db uses:
ALTER ROLE airflow_user SET search_path = airflow;
Did that, reloaded psql conf, rebooted DB server, whole machine - same error.
Switch to DB account with:
sudo -i -u postgres
Go to psql prompt:
psql
Connect to airflow db:
\c airflow_db
Run:
ALTER ROLE airflow_user SET search_path = airflow, public;
Now run
airflow init db
It should initialize db fine.

What's the difference between initdb /usr/local/var/[db] and createdb [db]

I am starting to use PostgreSQL and I am confused about the two ways to create a database. When I installed it the first time, the instructions said I have to create a default database with initdb /usr/local/var/postgres When I lookup my databases, I can see that I have a database called postgres. Now I am able to create a database with two other commands whereas the former is the command line script and the latter the SQL command. In the case of a "postgres" called database it would be:
createdb postgres
CREATE DATABASE postgres
Both are setting up a database in my list of databases. When I try to create another database with initdb /usr/local/var/[someDbName] though, it doesn't appear in my list of databases. So what's the difference between initdb and createdb then?
initdb is not used to create a "new database".
As documented in the manual you need it to create a "cluster" or "data directory" which then stores databases created with create database.
Quote from the manual:
Before you can do anything, you must initialize a database storage area on disk. We call this a database cluster. (The SQL standard uses the term catalog cluster.) A database cluster is a collection of databases that is managed by a single instance of a running database server
[...]
In file system terms, a database cluster is a single directory under which all data will be stored. We call this the data directory or data area
In short: initdb creates the necessary directory layout on the harddisk to be able to create and manage databases.
It's a necessary part of the installation process of a Postgres server.

Obtain aws cli command to create RDS instance from existing database

Is there a way to automatically generate the database creation string for an empty database with settings from an existing database?
I created an AWS RDS postgresql database instance using the console in my browser. I selected all kind of setting in my browser and launched the instance. Now I want to replicate this database (empty) with a command for programmatic use. I would like to delete the existing RDS instance so a raw command would be preferred (no reference to another RDS) but something like:
aws create-db-instance --db-name database02 --allocated-storage 200 --engine postgres etc.
You'll need to create a snapshot of the source Database, instance or cluster and then create a new database based on that snapshot. You can delete the source database after the snapshot is complete.
aws rds create-db-snapshot
--db-snapshot-identifier mydbsnapshot
--db-instance-identifier mydb
aws rds restore-db-instance-from-db-snapshot
--db-instance-identifier mynewdbinstance
--db-snapshot-identifier mydbsnapshot
http://docs.aws.amazon.com/cli/latest/reference/rds/create-db-snapshot.html
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_RestoreFromSnapshot.html
http://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-instance-from-db-snapshot.html