changing permission of table in postgresql - postgresql

I took postgresql dump and restore it in another db. I am trying to change the permission of the table in the secondary db. But I am not change the permission can't evn view individual table.
Whenever I list all the tables in a Scheme I can get the results but when i search for individual tables getting below error.
select \* from pg_tables where schemaname='testing';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
\------------+-----------------+------------+------------+------------+----------+-------------+-------------
testing | chatting | postgres | | t | f | t | f
testing | report | postgres | | t | f | f | f
select chatting from pg_tables where schemaname='testing';
ERROR: column "chatting" does not exist
LINE 1: select channel from pg_tables where schemaname='testing';
Can someone help me on this? I would like to change the owner of all the tables in this schema to a different user.
Note: I haven't create the tables, it is part of the pg_dump.

The query that you should run is
select * from pg_tables where schemaname='testing' and tablename = 'chatting';

Related

Postgres query is getting lock while executing through shell script

The below query is getting locked by executing through perl
ALTER TABLE base_table
DETACH PARTITION part_table;
By using this query
select pid,state,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;
pid | state | usename | blocked_by | blocked_query
-------+--------+----------+------------+------------------------------------
20521 | active | parttest | {20306} | ALTER TABLE BASE +
| | | | DETACH PARTITION part_table
| | | |
(1 row)
Can you please suggest me how to solve this issue?

Why does selecting all tables from postgres gives different results based on syntax used

If I log into postgres and switch to the postgres database, when I check for tables with this command I get nothing back:
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# \dt+
Did not find any relations.
However if I do a select on the DB tables I do see that the tables are there:
postgres=# SELECT *
postgres-# FROM pg_catalog.pg_tables
postgres-# WHERE schemaname != 'pg_catalog' AND
postgres-# schemaname != 'information_schema';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+----------------+------------+------------+------------+----------+-------------+-------------
pgagent | pga_jobclass | postgres | | t | f | t | f
pgagent | pga_job | postgres | | t | f | t | f
pgagent | pga_jobagent | postgres | | t | f | t | f
pgagent | pga_jobstep | postgres | | t | f | t | f
pgagent | pga_schedule | postgres | | t | f | t | f
pgagent | pga_exception | postgres | | t | f | t | f
pgagent | pga_joblog | postgres | | t | f | t | f
pgagent | pga_jobsteplog | postgres | | t | f | t | f
(8 rows)
Why do I get nothing when I use \dt+ but I can see the tables using a select statement?
I think that this will help LINK
\d [NAME] describe table, index, sequence, or view
\d{t|i|s|v|S} [PATTERN] ("+" detail) list tables/indexes/sequences/views/system tables
\da [PATTERN] list aggregate functions
\db [PATTERN] list tablespaces (add "+" for more detail)
use \dt+ *
the * to match all as the command needs a pattern
The reason is that the pgagent schema is not on your search_path. \dt will only show those tables that you can access without schema qualification.

postgREST can find relation

I'm trying to set up postgREST. Have been following the tutorial at http://postgrest.org/en/v5.1/tutorials/tut0.html. Here is what I see. First, the schemas:
entercarlson=# \dn
List of schemas
Name | Owner
--------+---------
api | carlson
public | carlson
Then a table:
carlson=# \d api.todos
Table "api.todos"
Column | Type | Collation | Nullable | Default
--------+--------------------------+-----------+----------+---------------------------------------
id | integer | | not null | nextval('api.todos_id_seq'::regclass)
done | boolean | | not null | false
task | text | | not null |
due | timestamp with time zone | | |
Indexes:
"todos_pkey" PRIMARY KEY, btree (id)
Finally, some data:
carlson=# select * from api.todos;
id | done | task | due
----+------+-------------------+-----
1 | f | finish tutorial 0 |
2 | f | pat self on back |
(2 rows)
But then I get this:
$ curl http://localhost:3000/todos
{"hint":null,"details":null,"code":"42P01","message":"relation
\"api.todos\" does not exist"}
Which is consistent with this:
carlson=# \d
Did not find any relations.
What am I doing wrong?
PS. I don't see which database this schema belongs to
Seems you're targeting the wrong database, check the db-uri config value and make sure this uri contains the api.todos table through psql.
Also, want to clarify that the search_path is modified by PostgREST on each request, so if you ALTER your connection user search_path it'll have no effect on the schemas PostgREST searches.

select table from AccessExclusiveLock locked table

I'm testing postgresql lock mechanism on my pgsql-9.1 on centos-5.8.
What I'm wondering is how can I select from table which is exclusively locked caused by <IDLE> in transaction uncommited.
The case is below...
In session1
postgres=# create table test_table(col1 char);
CREATE TABLE
postgres=# begin;
BEGIN
postgres=# insert into test_table values('1');
INSERT 0 1
--uncommited
In session2
postgres=# alter table test_table add column c2 char;
-- It has been locked....
in session3
postgres=# select t.relname, l.locktype,page,pid,virtualtransaction,mode,granted from pg_locks l, pg_stat_all_tables t where l.relation=t.relid order by relation asc;
relname | locktype | page | pid | virtualtransaction | mode | granted
--------------+----------+------+------+--------------------+---------------------+---------
pg_class | relation | | 9940 | 2/715754 | AccessShareLock | t
pg_index | relation | | 9940 | 2/715754 | AccessShareLock | t
pg_namespace | relation | | 9940 | 2/715754 | AccessShareLock | t
test_table | relation | | 9660 | 9/671042 | RowExclusiveLock | t
test_table | relation | | 9639 | 7/707191 | AccessExclusiveLock | f
(5 rows)
postgres=# select col1 from test_table;
--It's not possible to select from exclusively locked table
What I need is to get size of the relation regardless of locking happened.
I'm bushing around set transaction isolation level but I didn't catch any solutions till now.
Any advice would be very appreciated.
Thanks in advance.
Your table is not access exclusively locked, it has an AccessExclusiveLock pending but not granted, behind a granted AccessShareLock. Although it does have about the same effect, new AccessShareLock queue up behind it. They could in theory jump the queue and be granted, but that risks the starvation of AccessExclusiveLock requests so it is not done.
If an estimate is good enough, you could do
select reltuples from pg_class where oid='test_table'::regclass;
Otherwise, about the only options is to kill one of the two processes that is holding the table lock hostage, either the one holding the AccessShare or the one wanting the AccessExclusive. (Or hack the PostgreSQL source code.)

Can't rename table in RedShift

I run the following command through python3.3 psycopg2 package:
ALTER TABLE my_schema.tbl_old RENAME TO tbl_new
Before the command is run, my python code makes sure tbl_new does not exist.
However, I get the following error:
relation "tbl_new" already exists
Table structure:
bipilot=# select * from PG_TABLE_DEF where tablename='tbl_old';
schemaname | tablename | column | type | encoding | distkey | sortkey | notnull
------------+-----------+--------------+---------------+----------+---------+---------+---------
my_schema | tbl_old | price | numeric(10,2) | lzo | f | 0 | f
my_schema | tbl_old | price_date | date | none | f | 1 | f
my_schema | tbl_old | product_id | smallint | delta | f | 0 | f
No indices are defined.
Also, when I run the same RENAME command from psql shell, it works well.
Anyone can explain this strange behavior?
Thanks!!!
Problem found - bug in my python code - it was written 'tbl_Old' (instead of 'tbl_old').