How to read the records in user table? - postgresql

Recently, I want to use psql command line to remove records in user table, but I couldn't get the records inside by using the following command before I delete any of them:
postgres=> select * from user;
But the system returns the DB owner only.
So I am wondering, is the table contents are hidden.

The command used above is checking the database system's internal users. If you want to check client's external users, need to put a double quote on the table name.
postgres=> select * from "user";
Then the table data would be shown .

Related

Replacing schema name when sharing sql script with other users

When collaborating with colleagues I need to change the schema name every time I receive a SQL script (Postgres).
I am only an ordinary user of a corporate database (no permissions to change anything). Also, we are not allowed to create tables in PUBLIC schema. However, we can use (read-only) all the tables from BASE schema.
It is cumbersome for the team of users, where everybody is creating SQL scripts (mostly only for creating tables), which need to be shared amongst others. Every user has its own schema.
Is it possible to change the script below, where I will share the script to another user without the need for the other user to find/replace the schema, in this case, user1?
DROP TABLE IF EXISTS user1.table1;
CREATE TABLE user1.table1 AS
SELECT * FROM base.table1;
You can set the default schema at the start of the script (similar to what pg_dump generates):
set search_path = user1;
DROP TABLE IF EXISTS table1;
CREATE TABLE table1 AS
SELECT * FROM base.table1;
Because the search path was change to contain user1 as the first schema, tables will be searched in that schema when dropping and creating. And because the search path does not include any other schema, no other schema will be consulted.
If you
However the default search_path is "$user", public which means that any unqualified table will be searched or created in a schema with the same name as the current user.
Caution
Note that a DROP TABLE will drop the table in the first schema found in that case. So if table1 doesn't exist in the user's schema, but in the public schema, it would be dropped from the public schema. So for your use-case setting the path to exactly one schema might be more secure.

Fully qualified table name in postgreSQL select query

Simple question, however google can't help in reasonable time.
Ok, I have user table in my_db database with id column.
I want to run very simple query
SELECT id FROM user;
but it fails.
ERROR: column "id" does not exist LINE 1: SELECT id FROM user;
Can you imagine?
Ok, Running
SELECT * FROM user;
outputs the list of internal postgresql database users, which is nothing to do with my users, it's data from completely another [internal] database.
However, connection with my_db was established.
user is an internal function (and a reserved word) returning the currently logged in user.
To use that as your own identifier, you need to quote it:
select id
from "user"
or
select id
from public."user".
But you should really avoid reserved words as table names (or any name that requires quoting the identifier)
The following query can be rewritten as
SELECT id FROM my_db.public.user;
Where id is column, my_db is database, user is table name, public - is the schema. More about schemas:
http://www.postgresql.org/docs/9.1/static/ddl-schemas.html
So you don't have to rename the table name.

Best way to change the owner of a PostgreSQL database and their tables?

I am trying to change the owner of a PostgreSQL database (version > 8.2) and its tables.
I read this solution:
Modify OWNER on all tables simultaneously in PostgreSQL
But is this the best way to do it (for recent versions of PostgreSQL)?. It seems that there is a function REASSIGN OWNED which is better, but this one changes every database owned by the old_role, doesn't it? I only want it for one database.
Like this post:
REASSIGN OWNED BY for 1 specified database
I am not going to change the owner postgres, which is the best way nowadays?
Thank you in advance
According to the manual:
Because REASSIGN OWNED does not affect objects within other databases, it is usually necessary to execute this command in each database
which would seem to meet your requirements, although it also says the command would affect table spaces (which are not specific to the current database).
The second SO answer you linked applies to the special case of the postgres user, which owns the system catalogs. You cannot change the ownership of these.
The two methods that spring to mind for me are:
1) First alter the database name, and then perhaps right a quick script which changes the owner on the current tables to the old tables.
ALTER DATABASE <dbname> OWNER TO <newowner>;
\o altertable.sql
SELECT 'ALTER TABLE ' || table_name || ' OWNER TO <newowner>; ' FROM information_schema WHERE table_schema = <oldowner> and table_catalog = '<dbname>';
\o
\i altertable.sql
The above will generate your commands, then just pop them into a file and execute it.
2) The other option would be to use pg_dump to dump your database in plain text mode and then alter the appropriate strings using search and replace:
pg_dump -Fp -f dbbackup.dmp <dbname>
vi dbbackup.dmp
:%s/oldowner/newowner/g
save and exit
Hope this helps.

Sybase TSQL access to table or view

When I select all data from table/view person from database city I'll do it like this:
select * from city..person
ASE then substitutes the * to all the columns and .. for .dbo. and the query will be this:
select name, age, sex from city.dbo.person
If I have another view person created by another user (lets call it boss), and I want to access that view I need to make a select like this:
select * from city.boss.person
Is there a way to make the city..person to be city.boss.person instead of city.dbo.person?
The naming convention in Sybase to identify a table/view is [[database.]owner.]table_or_view_name, which means that the database and owner qualifiers are optional.
If you don't specify them, database is expanded to the current database and owner is expanded to the current user.
In your example, city..person expanded to city.dbo.person, because you're running under dbo user. The only way to have ASE expanding city..person to city.boss.person, is running the query under the boss user.

Advantage database how to retieve connection name

I am using Sybase Advantage, I have 2 tables:
The first table has the data records
The second table stores a history of the first
The first table has triggers to populate records in the second table depending on which fields get changed.
I would like to store the connection name (PC which made the request), the name that is displayed in the active queries page (Server Info dialog) and not the username. Does anyone know if this is possible?
Thanks
The following SQL statement can be used to retrieve the computer name instead of the user name.
SELECT *
FROM
( EXECUTE PROCEDURE sp_mgGetConnectedUsers() ) ConnUsers
WHERE
ConnUsers.DictionaryUser = USER();
The stored procedure sp_mgGetConnectedUsers is documented here.