Why can't non-superuser see data in stl_load_errors in Redshift? - amazon-redshift

The Amazon Redshift documentation for stl_load_errors states that "This table is visible to all users." However, I get different results when querying as a superuser (936 rows) vs a non-superuser (0 rows). Why does the query run as the non-superuser return 0 rows?
Here are the queries I ran.
Superuser:
$ psql -U masteruser -h XX.XX.XX.XX -p 5439 bi -w -c "select count(*) from stl_load_errors"
count
-------
936
(1 row)
Non-superuser:
$ psql -U emonsen -h XX.XX.XX.XX -p 5439 bi -w -c "select count(*) from stl_load_errors"
count
-------
0
(1 row)
Furthermore I can use HAS_TABLE_PRIVILEGE to show that Redshift thinks "emonsen" has the correct privileges on stl_load_errors:
$ psql -U masteruser -h XX.XX.XX.XX -p 5439 bi -w -c "select has_table_privilege('emonsen', 'stl_load_errors', 'select')"
has_table_privilege
---------------------
t
(1 row)

the solution here is to be granted with the SYSLOG ACCESS permissions. Here is the documentation: AWS - Visibility of Data in System Tables and Views

Any user can see all the stl_load_errors results of other users by granting access to syslog as shown below here.
alter user username SYSLOG ACCESS { UNRESTRICTED }

You are right! This table is visible to all users, but the catch here is Masteruser can see all the load errors across all users.
Whereas other users can see their own error logs only. In your case 'emonsen' can see his own error logs only, as in load error generated by 'emonsen'. Nothing wrong with privileges

Related

PostgreSQL pgbench error "pgbench: fatal: could not count number of branches: ERROR: relation "pgbench_branches" does not exist"

I am getting error like "pgbench: fatal: could not count number of branches: ERROR: relation "pgbench_branches" does not exist" when i ran the commmand "pgbench -c 20 -t 1000 -C -f d:\test_postgres\script.sql -p 5432 -U postgres -S Shiftlog" in windows command prompt.
-c is number of current clients
-t number of transactions per clients
-C Close the connection afer every transaction
-f run/execute the specified file
-p Port number of postgre instance
-S Database name in the postgre instance
I haven't stored the pgbench tables in my db(i.e. Shiftlog). Because i don't know the process to get these tables in windows postgres environment. Even though i specified the script file why pgbench is using the TPC-B file for the excurtion?.
D:\test_postgres>pgbench -c 20 -t 1000 -C -f d:\test_postgres\script.sql -p 5432 -U postgres -S Shiftlog
Password:
pgbench (14.5)
pgbench: fatal: could not count number of branches: ERROR: relation "pgbench_branches" does not exist
LINE 1: select count(*) from pgbench_branches
^
pgbench: Perhaps you need to do initialization ("pgbench -i") in database "Shiftlog"
D:\test_postgres>
Could you please help me to resolve this problem. Thank you so much for your support. Please check below image for the commands of sql script file.
enter image description here

PostgreSQL 13 :: ERROR: permission denied for table 'mydb'

Below I'm creating database mydb and populating it. Notice that the last step I perform is setting the password for postgres. This is simply to avoid password prompts during previous steps.
I followed steps in other StackOverflow posts, namely issuing the GRANT ALLs on TABLES, SEQUENCES and FUNCTIONS, but am still facing the below issue.
mydb.sh:
su - postgres <<xEOFx
set +H
psql -c "CREATE DATABASE mydb"
psql -c "CREATE USER user01 WITH ENCRYPTED PASSWORD 'SomePassword'"
psql -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public to user01"
psql -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public to user01"
psql -c "GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public to user01"
psql --dbname=mydb --username=postgres -f /tmp/mydb.sql
psql -c "GRANT ALL PRIVILEGES ON DATABASE mydb TO user01"
psql -c "ALTER USER postgres WITH PASSWORD 'AnotherPassword'"
exit
xEOFx
The script does not fail, but I also cannot access the mydb as user01:
jdoe$ psql --username=user01 --dbname=mydb
Password for user user01:
psql (13.2)
Type "help" for help.
mydb=> \c
You are now connected to database "mydb" as user "user01".
mydb=> \dt
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+----------
public | some_table | table | postgres
(1 rows)
mydb=> select * from some_table;
ERROR: permission denied for table some_table
mydb=>
SIDEBAR: I do notice that the owner of some_table is postgres, and would prefer that it be user01. Perhaps this could be part of the issue.
What am I missing?
Thank you in advance.
EDIT: Note that I tried running psql --dbname=mydb --username=postgres -f /tmp/mydb.sql before the GRANT ALL statements, too.
You are granting privileges to tables, sequences, and functions in the public schema in the database postgres, not mydb. By default, psql will connect to the database named the same as the current user, which is postgres in this case. Make sure you run the commands in mydb by adding -d mydb to your psql commands.

pg_dump ignoring schema specified with -n

I am using pg_dump (PostgreSQL) 9.2.4.
I have a database with two different schemas that have identical table names.
- mydatabase
- schema_a
- mytable
- someothertable
- schema_b
- mytable
- another table
I want to copy both schema_a.mytable and schema_b.mytable from orig_host to new_host. I log into new_host and type:
% psql -c "drop schema schema_a cascade" mydatabase
% psql -c "create schema schema_a" musicbrainz_db
% pg_dump -h orig_host -n schema_a -t mytable mydatabase | psql mydatabase
No problem, but when I do the same for schema_b, I get conflicts:
% psql -c "drop schema schema_b cascade" mydatabase
% psql -c "create schema schema_b" musicbrainz_db
% pg_dump -h orig_host -n schema_b -t mytable mydatabase | psql mydatabase
ERROR: relation "artist" already exists
I confirm by dumping this last command to a file that it is setting the search path to schema_a, which causes the failure. It does seem to work if I do
% pg_dump -h orig_host -t schema_b.mytable mydatabase | psql mydatabase
But shouldn't the -n switch work here?
From the manual -
The -n and -N switches have no effect when -t is used, because tables selected by -t will be dumped regardless of those switches, and non-table objects will not be dumped.
You might have schema_a in your search_path, which is why the first command works.
Take a look at the SQL produced. I believe what you should see is:
Everything in schema_a
All tables named *.mytable
That is, the options are additive.
I'm guessing the intent is that schema S might well depend on one or two other tables so you can dump them all together.

how to change default public schema on the psql command line

In order to load a MS Access mdb file into PostgreSQL, I type the following command on the psql command line.
mdb-schema xxx.mdb postgres | psql -h xxx -U xxx -W -d xxx
However, the Postgres tables are made under the default public schema. I want to have them under a different schema ("network"), rather than the default schema("public"). Could you please tell me how to change from "public" to "network" in this situation?
I appreciate any suggestions.
You need to set PGOPTIONS:
mdb-schema xxx.mdb postgres | PGOPTIONS='-c search_path=network' psql -h xxx -U xxx -W -d xxx
and here is the proof (set schema to test_schema):
$ PGOPTIONS='-c search_path=test_schema' psql postgres -c 'show search_path'
search_path
-------------
test_schema
(1 row)
Using PGOPTIONS you can set (almost) any configuration directive

Store PostgreSQL query result to Shell or PostgreSQL variable

For instance, I have a table stores value:
select * from myvalue;
val
-------
12345
(1 row)
How can I save this 12345 into a variable in postgresql or shell script?
Here's what I tried in my shell script:
var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
but echo $var gives me:
val ------- 12345 (1 row)
I've also tried
\set var (select * from myvalue)
in psql and when I type \set it lists:
var = '(select*frommyvalue)'
No, no, no! Use "raw data" switch from psql, like "-t" or "\t" and pipe the query to psql instead of parsing ascii-table, come on :-)
echo 'select * from myvalue;' | psql -t -h host -U user -d db
If you really need parse psql output, you could also use -H switch ( turns on HTML output ), and parse it with some perl module for parsing html tables, I used that once or twice.. Also, you may want to use a pgpass file and ~/.psqlrc for some defaults, like default DB to connect, when not specified.
psql has a -c/--command= option to accept SQL from the command line, and -t/--tuples-only option to control output formatting.
$ psql -c 'select 1+1'
?column?
----------
2
(1 row)
$ psql -t -c 'select 1+1'
2
$ VALUE=`psql -t -c 'select 1+1'`
$ echo $VALUE
2
var=`psql -Atc "select 1;"`
echo $var
1
In this answer I explain one way to do it, using a co-process to communicate back-and-forth with psql. That's overkill if all you need is to run a query and get a single result, but might be good to know if you're shell scripting with psql.
You can filter the result you get with your psql command:
var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
var=$(cut -d' ' -f3 <<<$var)
None of these worked for me, but this did:
median_avm=psql "host=${dps1000} port=#### dbname=### user=${reduser} password=${redpass}" -c "SELECT AVG(column) FROM db.table;" -t
using a source file with ${dps1000}, ${reduser}, ${redpass} defined and manually entering port and dbname