db2audit usage questions/clarifications - db2

Bonjour friends,
Does anyone have commands or steps to setup Db2 level auditing so we can look for which user(s) is using the database, when (timestamp) it was used and what that user did (eg which queries, tables, schema was accessed)?
Merci
Geraldine

Here is one of good article:
[Db2] Simple test case shell script for db2audit instance and database level auditing
https://www.ibm.com/support/pages/node/1075779
Excerpt of the SQLs in the article *
db2 -v "drop db $DBNAME"
db2 -v "create db $DBNAME"
db2 -v "connect to $DBNAME"
db2 -v "connect to $DBNAME user hoge using hoge"
Excerpt of the resutl of instance level auditing from that article *
STATUS DATABASE USERID
----------- -------- ----------
0 DB1 db2inst1
0 DB1 db2inst1
0 DB1 db2inst1
0 DB1 db2inst1
-30082 DB1 hoge
-----
Much more information on it! So, it might be recommended to review this article.

Related

PostgreSQL 13: create empty copy of database

I have a AWS RDS PostgreSQL 13 server with some databases. I have to create an empty copy of one database (empty means schema (tables, views, functions) + security (users, roles)).
Is pg_dump -s what I am looking for?
Thanks!
pg_dump -d db_name -s. You will also need to do pg_dumpall -g to get the global data e.g. roles. This will get all global data for the Postgres cluster, so you may have more then you need for the particular database.
Postgres allows the use of any existing database on the server as a template when creating a new database. I'm not sure whether pgAdmin gives you the option on the create database dialog but you should be able to execute the following in a query window if it doesn't:
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;
Still, you may get:
ERROR: source database "originaldb" is being accessed by other users
To disconnect all other users from the database, you can use this query:
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'originaldb' AND pid <> pg_backend_pid();

how to take backup of specific tables of a schema and restore on other schema

Suppose i have two databases in postgres
DB1
schema_abc
schema_xyz
...........
DB2
schema_lol
schema_qwx
Now i want to take backup of specific tables of schema_abc and restore them on schema_lol.
I am using pg_dump -U postgres DB1 --schema=schema_abc -t tbl1 -t tbl4 > file_name.sql
but search_path will be schema_abc in sql file, how can i restore this sql file to differently named schema of different db i.e schema_lol of DB2

Listing tables from imported database in PostgreSQL

I want to display the tables from the database that I have imported into the PostgreSQL database. I tried using \d but it shows no relations found.
The size of the database is around 2 GB.
Is there any other option to list the tables in the database?
Here is the set of statements I executed to import the database and to list the tables in the database
postgres=# create database projects;
CREATE DATABASE
postgres=# \c projects;
You are now connected to database "projects" as user "postgres".
projects=# psql -d projects -u postgres -f demo.sql
projects-# \c projects;
You are now connected to database "projects" as user "postgres".
projects-# \d
No relations found.
projects-#
psql is a command line program, not a SQL command.
Do not enter it at the psql prompt.
If you properly end every statement with a ; the line psql -d projects -u postgres -f demo.sql would have given you an error right away.
To run a script from inside psql use \i demo.sql

List all databases in PostgreSQL when there isn't a db with same name of the current user's

First of all, I am new to PostgreSQL.
So am I right thinking that one cannot run most of the psql util commands nor non-db-specified sql commands if there isn't a db with same name of the current user's?
That is saying e.g., if I run psql "show databases;" as user postgres while there isn't a db called "postgres", I won't be able to run the command.
Question is that in this case, one cannot find the list of the dbs before knowing any of db exits, is that how it works?
You have to connect to a database. By default, the databases "template1" and "postgres" will exist and will accept connections.
If your PostgreSQL admin has changed things in such a way that you can't connect to either of those databases, you'll have to do one of two things.
Ask the PostgreSQL admin what database you're supposed to connect to.
Create a database, then connect to it. There's more than one way to do this.
If you have CREATEDB privileges, you can create a database on the psql command line. For example, I have CREATEDB privileges here, so I can do this, which creates the database "mike" and exits.
$ psql -h localhost -p 5435 -U mike -c "create database mike"
Now I can connect to "mike" by either taking advantage of the default database name, or by specifying it.
$ psql -h localhost -p 5435 -U mike
$ psql -h localhost -p 5435 -U mike mike
You can. If you connect (with proper user, usually postgres) to the postgres database there are several tables on the pg_catalog (PostgreSQL) among those is pg_database table a simple select * from pg_database will show all databases.
Here is an image showing that on pgAdmin III Tool
There is no way of doing exactly what you want without, at least, knowing the database catalog. The postgres database is default and will exist in all installed instances (unless someone had droped it). All RDBMs is the same they all have the catalog (also named information_schema, or other names depending on vendor) which holds all information about the databases, tables, constraints, etc.

"No relations found" in psql after rails db:migrate succeeds

As a Rails novice, I'm following instructions in railscast #342 to set up a postgres database connection on my Mac.
I created a new rails project with
$ rails new blog -d postgresql
I edited the database yaml file to set the username and password.
I used psql to add the new user and password, and gave it permission to create tables: alter user blog create db
I created the db via
rake db:create:all
It succeeded and inside psql, doing \l to list schemas, I see all three schemas blog_test, blog_development and blog_production
I then do
$ rails g scaffold article name content:text
all looks good
I then do
$ rake db:migrate
I get messages showing success:
$ rake db:migrate
== 20150701220010 CreateArticles: migrating ===================================
-- create_table(:articles)
-> 0.0128s
== 20150701220010 CreateArticles: migrated (0.0129s) ==========================
I set my search path to look at the schema:
set search_path to lcuff,public,blog_development;
show search_path:
search_path
---------------------------------
lcuff, public, blog_development
But trying to find the table,
# \d
No relations found.
I've done the db:migrate VERSION=0 and it successfully reports that it drops the table, and then I create it again with db:migrate and it reports success.
If the first part hadn't worked, where it actually created the schema, I'd think I'm pointed to the wrong database somehow.
Ideas?
You should first connect to the database before fetching the tables.
\connect blog_development
And then try giving \d to list all tables.
You can also try with \dt.
Example(Tested in my Project):
\connect my_db
You are now connected to database "my_db" as user "postgres".
my_db=# \d
List of relations
Schema | Name | Type | Owner
--------+-------------------------------+-------+----------
public | access_managements | table | postgres
public | amenities | table | postgres
public | city_coordinates | table | postgres
public | coapplicants | table | postgres
Source