How to create Postgres database containing dot in it's name? - postgresql

I am trying to create the Postgres database using the terminal,
Normally we can create the database like
CREATE DATABASE mydatabase;
but I wanted to create with the database with a dot. like
CREATE DATABASE my.database;
I was trying to escape the characters. but it doesn't work
CREATE DATABASE my.\database;
CREATE DATABASE my\.database;
as an expected output, it should create the database named my.database

you should create the database using
create database "my.database";

Related

PostgreSQL rename table named with a keyword

I have a table named import.
I want to rename the table with the following statement in a sql script below.
Unfortunately I can't, because sql treats the term import as a psql keyword.
How can I change the name in a sql script?
I have a Database change management also called database migration or database upgrading. Database change management is the process of managing the change of a database over the course of an application's lifecycle. What could change in a database? The database structure (i.e. the tables), master data but even indices, triggers and stored procedures could be added, changed or deleted over time.
ALTER TABLE import
RENAME TO api_exchange;
I am aware I can change the table name with a PostgreSQL client, but I need to do it in a SQL script for postgreSQL 10 in order to keep my Database change management intact.
You can quote reserved words using double quotes:
-- \i tmp.sql
CREATE TABLE "select"(id integer);
ALTER TABLE "select"
RENAME TO api_exchange;
\d api_exchange

How to copy all my tables from a Postgres db to another one without pg_dump?

I need to copy all my tables from my Postgres database to another one. Usually I'd do it with pg_dump and then pg_restore, but unfortunately I don't have rds_superuser permissions and so it doesn't work.
Basically I have to copy the data table by table, create table in the other db and then import the data. This is how I would do if I'd have to do it manually.
Does anybody know how to do this in a programmatic way?
Thanks!
If you have createdb privilege, you can try using something like below.
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER your_user_name;
It will copy all your tables(with structure) and data to your new db from your old one.

Group many postgresql databases into separate schemas into same database

We have many postgresql databases with the same structure using only public shcema on each one.
How can I group all of them in a single database using separate schemas?
You can dump the database definition and data out, edit the output by putting the default schema as whatever you choose and run the scripts back into database.
Remember to make the dump in SQL format, pg_dump with default custom format won't work. The schema change will only need a change on a row like
SET search_path TO *whateverschema*
If you don't want to edit the dumps (maybe they're very large), you can of course also restore them one by one to the public schema, alter the tables into the desired schema and then repeat for the next one.
There is no special way to convert an existing database into a schema in another database unfortunately.
I forgot to post the answer afer all klin comment was the answer, this step was the solution,
Inside customer_x database:
alter schema public rename to customer_x;
And then take pg_dump customer_x:
pg_dump "customer_x" --schema "customer_x" -f customer_x.sql
Inside new conglomerated database:
DROP schema customer_x CASCADE;
create schema customer_x;
Then load the dump of customer_x:
psql "conglomerated_database" -f customer_x.sql

Copy a PostgreSQL database into another database

How to copy (or create new one and copy) a PostgreSQL database to another (new) database?
Is there any query based method for doing this?
Try This:
CREATE DATABASE newdb WITH TEMPLATE olddb;

How to delete database tables in postgres

I have created some databases with postgres, and put some data in them.
The problem is when I delete/drop database, and then create new database, the new database always contains tables and data from the first database that was created with postgres.
How can I delete database so that, when the new database is created it dosent contain data from old database?
Thanks
It sounds like you created tables in the template1 database (or you specify the TEMPLATE xyz option with your CREATE DATABASE statement).
To get rid of the tables in the template1 database, connect to it and drop all tables there. After that new database will not contain those tables any more.
Delete the rows in table using truncate commnad
truncate <tablename>
then delete database using drop command
drop database <databasename>