How do you wipe a Postgresql database? - database

I'm sending queries through Django on a PaaS service, and I think I can't access any command line utilities. I would have just dropped the entire database and recreated it, but I don't have permissions for that.
I'm looking for a simple command that would return the database to a completely virgin state.

you could cascade drop the schema and then drop the db:
drop schema myschema CASCADE;
drop database mydb;
if you do not have the rights to do so, you will have to drop table by table.
EDIT: If you can only drop tables, this will give you the SQL statements to run:
select 'drop table '||schemaname||'.'||tablename||' CASCADE;'
from pg_tables where schemaname = 'myschema' order by schemaname, tablename;

Related

snowflake role: not giving select access to tables moved into the schema from another schema

In snowflake i have a role given select access to all the tables inside a schema SCHEMA1
GRANT USAGE ON DATABASE DB1 TO test_role;
GRANT USAGE ON SCHEMA DB1.SCHEMA1 TO test_role;
GRANT SELECT ON ALL TABLES IN SCHEMA DB1.SCHEMA1 TO ROLE test_role;
I have three tables inside them
but when I a move a table into this schema from another schema its not getting the select access
alter table if exists "DB1"."SCHEMA2"."table1" RENAME TO "DB1"."SCHEMA1"."table4";
i am expecting all tables to have select access inside this schema "SCHEMA1".
Also i observed those created inside the schema directly are having select access, but those which have been moved using alter are not getting the select access.
i am expecting all tables to have select access inside this schema
"SCHEMA1".
Also i observed those created inside the schema directly are having
select access, but those which have been moved using alter are not
getting the select access.
When you create a table, it doesn't come with additional grants (such as SELECT) except ownership. You said that the tables created inside the schema have SELECT access, so I suspect FUTURE GRANTS are applied for that schema.
The rename operation doesn't trigger "future grants":
https://docs.snowflake.com/en/sql-reference/sql/grant-privilege.html#restrictions-limitations
In this case, you need to grant the required SELECT permissions.

My query sys.database_audit_specification does't send back any record

I'm studying about SQL Server Audit. I have deployed Server Audit Specification. Now I want to query all the records but It doesn't return anything.
I use Windows Server 2012 Datacenter - SQL Server 2014 Developer Version
use master
go
select *
from sys.database_audit_specifications;
go
I got no output and don't understand why.
How can I fix it?
Here is an example that creates a server-level audit, then adds a database-level audit specification to track multiple operations on any object in the dbo schema.
USE master;
GO
-- create aserver audit
CREATE SERVER AUDIT Test_Server_Audit
TO FILE ( FILEPATH = 'C:\temp\' ); -- you may need to change that'
GO
-- turn it on
ALTER SERVER AUDIT Test_Server_Audit WITH (STATE = ON);
GO
-- create a demo database
CREATE DATABASE floob;
GO
USE floob;
GO
CREATE TABLE dbo.blat(x INT);
GO
-- create a database audit specification that monitors for activity
-- against any dbo object:
CREATE DATABASE AUDIT SPECIFICATION Test_Database_Audit
FOR SERVER AUDIT Test_Server_Audit
ADD (SELECT, UPDATE, DELETE, INSERT, EXECUTE ON SCHEMA::dbo BY PUBLIC)
WITH (STATE = ON);
GO
-- do a couple of things:
SELECT * FROM dbo.blat;
DELETE dbo.blat;
GO
-- you should see those couple of things in the audit file:
SELECT * FROM sys.fn_get_audit_file('C:\temp\*.sqlaudit', NULL, NULL);
GO
For Further Reading follow this

What explicit permission am I missing that is preventing the effective INSERT permission at the table level?

I have a user that cannot update stored procedures because he gets an error that an INSERT cannot be done on one of our changelog tables. We checked his permissions to insert on that table and he has it, so we were confused as to why he couldn't insert. When we dug deep into this, it looks like we gave him the explicit permission to insert into the table, but for some reason he does not list INSERT as an effective permission. Our layout is server -> database -> table (pretty simple setup).
Note - for some reason when writing this question, it kept interpreting my inserted pictures as code and wouldn't let me post with embedded pics, so I had to use links instead.
Explicit server permissions:
ALTER ANY DATABASE
CONNECT SQL
CREATE ANY DATABASE
Effective server permissions:
ALTER ANY DATABASE
CONNECT SQL
CREATE ANY DATABASE
VIEW ANY DATABASE
VIEW ANY DEFINITION
Explicit database permissions:
ALTER
CONNECT
CREATE TABLE
DELETE
INSERT
SELECT
SHOW PLAN
UPDATE
VIEW DEFINITION
Effective database permissions:
Explicit table permissions:
ALTER
CONTROL
DELETE
INSERT
REFERENCES
SELECT
TAKE OWNERSHIP
UPDATE
VIEW CHANGE TRACKING
VIEW DEFINITION
Effective table permissions:

ALTER DATABASE failed because a lock could not be placed on database <MyDB>

I have a program need to create and drop a database multiple time, but sometime when dropping a database I get a exception here:
ALTER DATABASE failed because a lock could not be placed on database ...
And the command is like this:
USE master;
IF EXISTS(select * from sys.databases where name='{0}')
BEGIN
ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [{0}]
END
Why it happens?
What is the better way to do this (drop database)?
You can't DROP a database if any one else is connected to it. Simply running DROP DATABASE MyDatabase; doesn't close those connections, thus the DROP fails.
Changing the database to SINGLE USER will drop any existing connections (WITH ROLLBACK IMMEDIATE causes any transactions to be rolled back immediately, which is a problem here, as your about the DROP the database). Then, because it's instantly the next statement, the database is dropped before anyone gets the opportunity to reconnect.

Clear logs from SQL Server Audit File

Is it possible to Clear SQL Server Audit File logs. I want to delete old logs by date but can't find a way to delete it both from interface and sql Query.
Yes, it is.
By sys.server_audits select old names via create_date column.
Then loop the names and delete them by using the next code for deleting:
ALTER SERVER AUDIT [Audit_name] WITH (STATE = OFF)
GO
USE [master]
GO
DROP SERVER AUDIT [Audit_name]
GO

Resources