PostgreSQL command does not list all schemas - postgresql

I'm wondering why \d only lists tables in the public schema? I have another schema in the database, sps, but those tables are not listed...
# psql -p 5432 -U postgres -h localhost myDB
Password for user postgres:
psql (9.1.5)
Type "help" for help.
myDB=# \d
List of relations
Schema | Name | Type | Owner
--------+----------------------------+----------+----------
public | tableA | table | postgres
public | tableB | table | postgres
public | tableC | table | postgres
public | table_col_seq | sequence | postgres
(4 rows)

You need to change your search_path. In psql use \dn and then build your search_path:
SET search_path = schema1,schema2,public;

I had this problem i Intellij IDEA Database tab.
I got the soultion doing :
Data Sources and Drivers -> YOUR_DATA_SOURCE -> Schemas -> Check "All databases"

Related

User permission to list databases

I have exported all databases using pg_dumpall, then I imported them using psql. for the database "command" the are 2 users, "jo" is the owner and "ko" can manipulate all tables(list, edit, delete).
when I connect to the database using the user "jo" psql -U jo -h localhost -d command I can list all tables.
I run the command \z login I get
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+-------+-------+--------------------+-------------------+----------
command| login | table | jo=arwdDxt/jo +| |
| | | ko=arwdDxt/jo +| |
but when I connect using "ko" psql -U ko -h localhost -d command, I run the \dt I get
Did not find any relations.
when I run select * from command.login; I get the data inside the login table, when run select * from login I get the error
ERROR: relation "login" does not exist
LINE 1: select * from login;
How can I list the tables using the user "ko" ?
Simple: put schema command on the search_path and give user ko USAGE permissions on the schema.

Validating the data inside postgres table

I have launched a postgres container. By injecting script.sql at docker entrypoint, I created database, schema, tables and have inserted data into them. Docker Logs says that all table creation and data insertion is successful .
But How can I validate the data insertion? Below commands didn't help
List of relations
Schema | Name | Type | Owner
------------+----------------------+-------+----------
my_db | users | table | postgres
my_db | audit_log | table | postgres
my_db | config | table | postgres
(3 rows)
my_db=# SELECT * FROM my_db.users
my_db-# SELECT * FROM users
my_db-# SELECT * FROM my_db.users;
What is wrong here? Please help.
You should use
\c my_db
to connect to your database. And then:
SELECT * FROM users;
To query your table into your database.
It seems that you understood this part, but everything should be done using postgresql command line, so inside the docker container.

How to select a schema in postgres when using psql?

I have a postgres database with multiple schemas. When I connect to the database from a shell with psql and I run \dt it uses the default connection schema which is public. Is there a flag I can specify or how can I change the schema?
In PostgreSQL the system determines which table is meant by following a search path, which is a list of schemas to look in.
The first matching table in the search path is taken to be the one wanted, otherwise, if there is no match a error is raised, even if matching table names exist in other schemas in the database.
To show the current search path you can use the following command:
SHOW search_path;
And to put the new schema in the path, you could use:
SET search_path TO myschema;
Or if you want multiple schemas:
SET search_path TO myschema, public;
Reference: https://www.postgresql.org/docs/current/static/ddl-schemas.html
\l - Display database
\c - Connect to database
\dn - List schemas
\dt - List tables inside public schemas
\dt schema1. - List tables inside particular schemas. For eg: 'schema1'.
Do you want to change database?
\l - to display databases
\c - connect to new database
Update.
I've read again your question.
To display schemas
\dn - list of schemas
To change schema, you can try
SET search_path TO
if you in psql just type
SET schema 'temp';
and after that \d shows all relations in "temp
Use schema name with period in psql command to obtain information about this schema.
Setup:
test=# create schema test_schema;
CREATE SCHEMA
test=# create table test_schema.test_table (id int);
CREATE TABLE
test=# create table test_schema.test_table_2 (id int);
CREATE TABLE
Show list of relations in test_schema:
test=# \dt test_schema.
List of relations
Schema | Name | Type | Owner
-------------+--------------+-------+----------
test_schema | test_table | table | postgres
test_schema | test_table_2 | table | postgres
(2 rows)
Show test_schema.test_table definition:
test=# \d test_schema.test_table
Table "test_schema.test_table"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
Show all tables in test_schema:
test=# \d test_schema.
Table "test_schema.test_table"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
Table "test_schema.test_table_2"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
etc...
This is old, but I put exports in my alias for connecting to the db:
alias schema_one.con="PGOPTIONS='--search_path=schema_one' psql -h host -U user -d database etc"
And for another schema:
alias schema_two.con="PGOPTIONS='--search_path=schema_two' psql -h host -U user -d database etc"
key word :
SET search_path TO
example :
SET search_path TO your_schema_name;
quick solution could be:
SELECT your_db_column_name from "your_db_schema_name"."your_db_tabel_name";
if playing with psql inside docker exec it like this:
docker exec -e "PGOPTIONS=--search_path=<your_schema>" -it docker_pg psql -U user db_name
PostgreSQL 14 Debian
postgres#ovhswift:~$ psql
psql (14.0 (Debian 14.0-1.pgdg100+1))
Type "help" for help.
postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create schema tests;
CREATE SCHEMA
test=# \dt
Did not find any relations.
test=# create table pubtable (id integer);
CREATE TABLE
test=# create table tests.schematable (id integer);
CREATE TABLE
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | pubtable | table | postgres
(1 row)
test=# \dt tests.
Did not find any relation named "tests.".
test=# \dt tests
Did not find any relation named "tests".
test=# \dt 'tests.'
Did not find any relation named "tests.".
test=# \dt 'tests.*'
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+----------
tests | schematable | table | postgres
(1 row)
test=# \dt 'tests*'
Did not find any relation named "tests*".
test=# \dt 'tests.*'
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+----------
tests | schematable | table | postgres
(1 row)
Ditto for \dv etc. to see the views in the schema

Why aren't these commands creating a read-only user in PostgreSQL?

I've carefully reviewed similar questions on this site and I can't get it to work.
I'm using PostgreSQL 9.3.9 (installed from package) on Ubuntu 14.04 (Vagrant box).
I'm creating a new database called site owned by postgres.
I want to create a read-only user called readonly with access to this database.
I've tried to lock them out of SCHEMA public but whatever I try to do, they can still create tables.
Can anybody help me understand why this user can still write to the database, despite being locked out completely? Here's the steps I'm taking; first, set up the entities and explicitly lock out all the permissions:
postgres=# CREATE DATABASE site;
CREATE DATABASE
postgres=# CREATE ROLE readonly UNENCRYPTED PASSWORD 'password' LOGIN NOCREATEROLE NOCREATEDB NOSUPERUSER CONNECTION LIMIT -1;
CREATE ROLE
postgres=# REVOKE ALL ON SCHEMA public FROM public;
REVOKE
postgres=# REVOKE ALL ON SCHEMA public FROM readonly;
REVOKE
postgres=# REVOKE ALL ON DATABASE site FROM public;
REVOKE
Let's check that readonly can't connect:
vagrant#vagrant-web1:~$ PGHOST=127.0.0.1 PGPASSWORD=password psql -U readonly site
psql: FATAL: permission denied for database "site"
DETAIL: User does not have CONNECT privilege.
Everything correct up to this point. Now the superuser grants a single permission:
postgres=# GRANT CONNECT ON DATABASE site TO readonly;
GRANT
This is where the system breaks. The readonly user can log in and can create tables:
vagrant#vagrant-web1:~$ PGHOST=127.0.0.1 PGPASSWORD=password psql -U readonly site
psql (9.3.9)
SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.
site=> CREATE TABLE orders(id int);
CREATE TABLE
site=> \dt
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | orders | table | readonly
(1 row)
This has had me stuck for a couple of hours. Any help would be greatly appreciated.
Extra information: There's only a single schema in the database. Here's what the output of \du is:
postgres=# \du
List of roles
Role name | Attributes | Member of
---------------------+------------------------------------------------+-----------------------------------------------
postgres | Superuser, Create role, Create DB, Replication | {}
readonly | | {}
Here's the output of \list:
postgres=# \list
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------------+---------------------+----------+-------------+-------------+---------------------------------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
site | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres +
| | | | | readonly=c/postgres
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
You issued commands REVOKE ALL ON SCHEMA public ... in database postgres, not site. Check it:
site=> \dn+
List of schemas
Name | Owner | Access privileges | Description
--------+----------+----------------------+------------------------
public | postgres | postgres=UC/postgres+| standard public schema
| | =UC/postgres |
(1 row)
Line =UC/postgres means that public has Usage and Create privileges for schema public.
So do the following:
site=> \c site postgres
You are now connected to database "site" as user "postgres".
site=# REVOKE ALL ON SCHEMA public FROM public;
REVOKE
And now it works:
site=# \c site readonly
You are now connected to database "site" as user "readonly".
site=> create table t(n numeric);
ERROR: no schema has been selected to create in
site=> create table public.t(n numeric);
ERROR: permission denied for schema public

create new role?

I'm a newbie to PostgreSQL.
I'm trying to create a new role named pgdba like below...
localhost.localdomain:[/home/postgres]createuser -d -i -l -P -r -s pgdba
new role's password : pgdba
retype password : pgdba
password : postgres
localhost.localdomain:[/home/postgres]psql
Password: postgres
psql (9.0.1)
Type "help" for help.
postgres=# select * from pg_shadow;
usename | usesysid | usecreatedb | usesuper | usecatupd | passwd | valuntil | useconfig
----------+----------+-------------+----------+-----------+-------------------------------------+----------+-----------
postgres | 10 | t | t | t | md53175bce1d3201d16594cebf9d7eb3f9d | |
pgdba | 16385 | t | t | t | md53cd688326933adcdedd77097c95d131d | |
(2 rows)
postgres=# \q
localhost.localdomain:[/home/postgres]psql -U pgdba
Password for user pgdba: pgdba
FATAL: database "pgdba" does not exist
psql: FATAL: database "pgdba" does not exist
What am I doing wrong?
See how the message is that database pgdba does not exist?
By default, PostgreSQL will connect to a database named after the current user (see the psql docs. Since you aren't specifying a database to connect to, it's trying to connect to a DB named pgdba - which doesn't exist.
Try:
psql -U pgdba postgres
to connect to the postgres database using role pgdba.
You may also need to examine pg_hba.conf (see docs if you wish to tweak the authentication rules.
BTW, you are running an outdated PostgreSQL version with known bugs. Please update to 9.0.8, the latest patch release in the 9.0 series. You do not need to dump and reload your database or use pg_upgrade for this, it's a simple drop-in replacement. See the release notes for all the fixes you're missing.
Also, rather than SELECTing from pg_shadow, consider using psql's user information functions \du and \dg, which produce much better formatted and integrated information.