PostgreSQL create a schema under a different database using a script - sql

So what I want to do here is to run a script while connected to a database I already had using pgAdmin3. The script contains a create role, tablespace, database and a create schema and several tables under that schema.
The problem here is that when I run the script it creates the new role, tablespace and database correctly. It also creates the schema and the tables correctly but with a problem, the schema is created under the database, from which I ran the script, instead of the newly created database. The script is more or less like this.
CREATE ROLE "new_role" ... ;
CREATE TABLESPACE "new_space"
OWNER "new_role"
LOCATION '/home/...';
CREATE DATABASE "new_db"
WITH OWNER = "new_role"
TABLESPACE = "new_space";
CREATE SCHEMA "schema" AUTHORIZATION "new_role" ;
CREATE TABLE IF NOT EXISTS "schema"."new_table"(
...
) TABLESPACE "new_space";...
...
I already saw a solution with a \connect foo; but that is not what I wanted, I wanted it to somehow connect within the script without running things separately and running \connect foo in the terminal.
Can anyone tell me if there is anyway to do this and help me come out with a solution to this problem?

Use psql and split it up into two scripts . You can save the scripts in .sql files, and then run psql to connect to the DB you want to run each script against all on the same command line (with && in between each command). The two psql commands could be combined into one bash script so it's only one command that you need to run.
Something like this, if the script were named foo.sql:
psql -X -h <host> -U <user> -p <port> -f foo.sql <db_name>
The first script could have the create role, create tablespace and create database commands, connecting to the postgres db or a template DB, and the second script could have the rest of the commands.
You could also use createdb from the bash script instead of CREATE DATABASE.

Using pgAdminIV:
1- right click on default database "postgres"
2- select create database, give a name f.e. "newdatabase"
3- click on "newdatabase" (to establish connection)
4- open the query tool
5- import, write or paste your code
6- run your code f.e.: CREATE SCHEMA newschema;
It works for me...

Related

Create a database in sql*plus (sql command line)

I've used mysql shell for creating database (create database testdb), why can't i make a database in sql*plus command line. I also want to see the lists of database but i can't find queries anywhere. Please guide
SQL> create database testdb;
create database testdb
*
ERROR at line 1:
ORA-01501: CREATE DATABASE failed
ORA-01100: database already mounted
SQL>

How to connect from one schema to another schema of same database automatically when running a .sql file?

I am running a test.sql file from command prompt by connecting to the database named DB. The test.sql file contains DDL, DML statments in it.
The Schemas named A and B are used in the test.sql file.
First connection to the schema connect&&schema A was made. The DDL, DML statements for schema A got executed.
Next connect&&schema B was made in that same test.sql file. So the script at run time asks for the value of schema name B. After the connection to schema B then the DDL, DML statements for schema B will be executed.
Please suggest if I can connect automatically to schema B without entering the value of schema B name at run time when running the test.sql file.

expdp on a different SID and use of EXCLUDE, INCLUDE and CONTENT switch

I am trying to import a db into my local xe instance. I have a problem in that it is greater than 11GB and things fall over. My db has audit tables (courtesy of envers). I do not need this data.
Two questions here:
can I expdp table structures and not data for the aud_ tables that are my audit tables
can I expdp on a different SID. My SID is not orcl but orcllo (for historical reason)
For the second question I have done the following but
sqlplus / as sysdba#orcllo
alter user MY_DB identified by MY_PASS ACCOUNT UNLOCK;
CREATE OR REPLACE DIRECTORY db_dumps AS '/tmp/db_dumps';
GRANT READ, WRITE ON DIRECTORY db_dumps TO MY_DB;
but when I run expdp I get an error about db_dumps not being found.
Thanks
Q.1:
You can use 'EXCLUDE' or 'INCLUDE' switch during oracle impdp (data pump) to escape/include few tables.
The following code shows how they can be used as command line parameters.
impdp scott/tiger#db10g schemas=SCOTT exclude=TABLE:"IN ('EMP', 'DEPT')" directory=TEST_DIR dumpfile=SCOTT.dmp logfile=expdpSCOTT.log
Same can be done during export as well:
expdp scott/tiger#db10g schemas=SCOTT exclude=TABLE:"IN ('EMP', 'DEPT')" directory=TEST_DIR
CONTENT=METADATA_ONLY switch should be used if you indeed want to create structure on target schema/db.
Q.2:
You can not use expdp / impdp on a different SID as is but via DB Link route (using NETWORK_LINK switch).
However, i think, Ans-1 should address your real problem due to Oracle XE limitations. Moreover, there may be performance issues due to cross network/DB data flow.
Both the local and remote users require the IMP_FULL_DATABASE role granted to them.
impdp test/test#db10g tables=SCOTT.EMP network_link=REMOTE_SCOTT directory=TEST_DIR logfile=impdpSCOTT.log remap_schema=SCOTT:TEST
HTH
Check the following for more options with data pump:
http://www.oracle-base.com/articles/10g/oracle-data-pump-10g.php

Purge postgresql database as it was new

I have created a PostgreSQL database dump using psql.
Now I want to restore this backup from the file:
psql -d thesamename -f /my/backup/file
But I get errors that the data already exists.
Is there any command to delete everything from the database to bring it to just created state, except dropping and creating once again?
(I don't want to set up owner, tablescpace etc. once again)
Maybe some way to overwrite the database with the one from the backup file? (the backup file is from another database server)
https://www.postgresql.org/docs/current/static/app-pgdump.html
https://www.postgresql.org/docs/current/static/app-pgrestore.html
Have a look specifically at the -c option for these scripts.
You can drop the public schema of your database and re create it after :
DROP SCHEMA PUBLIC CASCADE;
CREATE SCHEMA PUBLIC AUTHORIZATION postgres;
GRANT ALL ON SCHEMA public TO public;
Hope it helps…
you can manipulate it as you want..and you want only dumping of data.once you will start to dumping data schema file will generate automatically.and no need to drop old data.it will overlap with new data over existing data.
You can go through this link for more clarification..
http://www.postgresql.org/docs/8.4/interactive/app-pgdump.html
http://www.postgresql.org/docs/8.4/interactive/app-pgrestore.html
You can use
GRANT ALL ON SCHEMA public TO public;

How to empty a SQL database?

I'm searching for a simple way to delete all data from a database and keep the structure (table, relationship, etc...).
I using postgreSQL but I think, if there a command to do that, it's not specific to postgres.
Thanks,
Damien
Dump the schema using pg_dump. drop the database, recreate it and load the schema.
Dump you database schema (the -s tag) to a file:
pg_dump -s -f db.dump DB-NAME
Delete the database:
dropdb DB-NAME
Recreate it:
createdb DB-NAME
Restore the schema only:
pg_restore db.dump > psql DB-NAME
This should work on PostgreSQL; Other DBMS might have their own tools for that. I do no know of any generic tool to do it.
EDIT:
Following comments, you might want to skip the dropdb command, and simply create another database with the dumped schema. If all went through well, you can drop the old database:
pg_dump -s -f db.dump DB-NAME
createdb DB-NEW-NAME
pg_restore db.dump > psql DB-NEW-NAME
At this point, you have the full database at DB-NAME, and an empty schema at DB-NEW-NAME. after you're sure everything is OK, use dropdb DB-NAME.
You can do something like this:
export PGUSER=your_pg_user
export PGHOST=database.host
export PGPORT=port
export PGDATABASE=your_database
psql -qAtX -c "select 'TRUNCATE table ' || quote_ident(table_schema) || '.' || quote_ident(table_name) || ' CASCADE;' from information_schema.tables where table_type = 'BASE TABLE' and not table_schema ~ '^(information_schema|pg_.*)$'" | psql -qAtX
It will do what's necessary.
Of course these exports are not necessary, but they will make it simpler to run 2 copies of psql without having to givem them all standard -U, -d, and so on, switches.
One thing though - using TRUNCATE to do so, while faster than DELETE, has it's drowbacks - for example - it is not being replicated by Slony and any other replication system that works on triggers. Unless you are working on PostgreSQL 8.4, and your replication knows how to use triggers on TRUNCATE.
I'm not a Postgres guy, but one option would be to iterate through the tables and issue a Truncate command against each one. You'll have to take otable relationships into account, though - you will not be able to delete reference data before data that refers to it, for example.
In pgAdmin you can do:
Right-click database -> backup, select "Schema only"
Drop the database
Create a new database and name it like the former
Right-click the new database -> restore -> select the backup, select "Schema only"
your can delete all records of your database without restriction of foreign keys by following three steps
Take script of your Database
Right Click on your database (your DB Name)
click on task and then "Generate script"
Specify location
Delete your database base
recreate a database with the same name and run you generated script
This way you can empty all of your database