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;
Related
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...
Is there a query in db2 9.7 control center wherein I can't DELETE(DROP) all the contents of my schema (including the schema) at once?
My other option is to drop/delete the objects first and then DROP schema..
But I want to DROP THE ENTIRE SCHEMA WITH ALL OBJECTS at once.
DROP SCHEMA <schema_name> CASCADE/RESTRICT didn't work for me.
The ADMIN_DROP_SCHEMA procedure is what you're looking for.
The ADMIN_DROP_SCHEMA procedure is used to drop a specific schema and all objects contained in it.
http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0022036.htm
First drop all the tables in the schema.
Then try to delete the schema using
DROP SCHEMA SCHEMA_NAME RESTRICT
webchain.in have sample java program, explains how to delete the schema using java program
in case drop schema fails after dropping all the tables with the error SQLCODE=-551, SQLSTATE=42501, try command
grant dbadm on database to USER_NAME
I need to write an sql script that creates both a new database AND a new schema in the database I just created.
How can I do it? Can I somehow change the current database to the new one? Or can I somehow specify the database for CREATE SCHEMA?
I'm using PostgreSQL 9.0
You can connect to the database, and execute the "CREATE SCHEMA" statement. That should result in a new schema in that database. It's not as tough as you think ;) When you want to do this from a .SQL file instead, you can use the \connect command as such:
CREATE DATABASE foo;
\connect foo;
CREATE SCHEMA yourschema;
Login to New-Database with new user:
postgres=> \connect newdb user1
...
You are now connected to database "newdb" as user "user1".
newdb=>
To create schema with new user "user1" in newdb:
newdb=> CREATE SCHEMA s1;
To list the schema :
SELECT * from information_schema.schemata;
Create database using
--CREATE DATABASE test;
Enter to the test database using
--psql -d test;
Create your schema in test database using
--create schema if not exists test_schema;
I am installing an application that requires me to set up a SQL Server DB with a schema. According to the SSMS 2008 documentation, after creating the DB I can expand the DB in the tree then right click on Security and I should have an option New Schema but I only have New User, Database Role.. and Application Role..
I tried just doing it with T-SQL:
use myDB;
create schema mySchema authorization db_owner
The command succeeded so I would expect after this that if I create a table, the Schema drop down list should include mySchema as an option but it doesn't.
Any ideas?
You need to refresh it. Highlight the Schemas folder and press F5.
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