Why command \dt gives - no relations found? - postgresql

I have created a database in postgres. It has 3 empty tables. The table has user tom as its Superuser along with root. I am logged in as tom and connected to mydb database. But still the commands \d or \dt - gives no relations found.
Is there any alternate to SHOW TABLE in postgresql?
Could not find solution here
I get results from - \dt *.*.

It is not a problem with your search_path, it could be an issue with your schema permissions as described in the answer here. Check with \dn+ that the public schema indicates permissions for the postgres role, and if not, grant them with: GRANT ALL ON SCHEMA public TO public;

I solved my problem by using double quote e.g \d "Table_name". Because my table name is capitalized like Foo, Bar. Hope that could help someones.

You might not be connected to the right database.
The first command \c DATABASE_NAME, coming from the following comment on Reddit, did the trick for me.
Make sure you're connected to the correct database with \c . Using \l will list all databases regardless of which database you're connected to, but most commands are specific to the connected database.
Use \dt .* to list all tables with name matching in all schemas.
Assuming the table exists, it will tell you what schema it's in. Then you can query with SELECT * FROM .;.
If you want to avoid having to explicitly specify the schema name, make sure the schema that contains the table is in your path: SHOW search_path and SET search_path .

If you have different instances of postgres running on server on different port it helps when you connect to database with specific port psql -p 5432 databasename( or psql -p 5433 databasename etc.)

you must have not added a semicolon at the end that's what always happens to me I always forget my semicolon -> create table nameoftable()

This happened for me when I removed the default privileges for PUBLIC on the public schema (specifically the USAGE privilege).

I had the exact same issue. None of the answers above helped. I was able to see my table when I ran \dt .*. Then I realized I had to call explicitly state its schema when running SELECT statements.
So, if you run SELECT * FROM public.<your_tablename_goes_here>; it should work. I hope this helps!

Related

Why doesn't psql show my data even though pgadmin has it

I have a database called demo which has 4 tables.
I run psql -U user1 demo and am able to login, but I cannot see any of the tables.
One of the table is student. I can surely just reimport the data all again, but why wouldn't it show here?
My user1 is full admin and has access to everything, so I don't think it's a user related issue.
All my tables are also owned by user1, if this makes any difference.
I am not using a VM, everything is on my local machine.
You need to add university schema to your search_path. Either you can SET search_path TO ... or add it to search_path in postgresql.conf. Otherwise, you could perform your SELECT with fully-qualified table name:
SELECT * FROM university.student;
Disclosure: I work for EnterpriseDB (EDB)
Your tables are located in the schema university you need to use:
select *
from university.student;
To list the tables in that schema, use: \d university.*

Querying postgresql using psql command in bash throws ERROR: relation "testschema.testtable" does not exist

On running the below command in bash throws:
ERROR: relation "testschema.testtable" does not exist.
Command line:
psql -h "localhost" -d "postgres" -U "postgres" -c "select * from TestSchema.TestTable;"
Why is it searching relation rather than schema?
How can I set default schema to be searched, and can I set a list of multiple schemas from multiple databases as default list?
Why is it searching relation rather than schema?
TestSchema.TestTable is a relation (table). I is a fully qualified table name.
The reason it's not finding it, is most probably because you created the schema and the table using doublequotes:
create table "TestSchema"."TestTable" (...)
which makes the names case sensitive and forces you to always use quotes to qualify the name. Please see the manual for details about "delimited identifiers" (aka "quoted identifiers):
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
How can I set default schema to be searched
See the "set schema" statement: https://www.postgresql.org/docs/current/sql-set.html
You can also define a default schema using the ALTER USER command.
can I set a list of multiple schemas from multiple databases as default list
Yes, set the search_path configuration variable: http://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-SEARCH-PATH
This can also be set permanently through an ALTER USER statement.

What is a valid PostgreSQL database name?

I create a database with a hyphen in the middle of the name with createdb. That successfully creates the database, but within the psql interactive client, I get a syntax error if I try a command like this:
ALTER DATABASE my-database SET SCHEMA = myschema,public;
psql complains of a syntax error at or near "-"
Is there some documentation for what counts as a valid PostgreSQL database name?
Should I just underscores instead of hyphens?
The documentation you asked about is here:
http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Most people just stick to lowercase letters, numeric digits, and underscores -- to avoid typing the quotes all the time.
Try putting it in double quotes:
ALTER DATABASE "my-database" SET SCHEMA = myschema,public;
I faced one issue and above answers helped me. So sharing scenario on dbname
Scenario:I was tried to change database name using PG admin III. My database name was My_Database
running below queries failed:
ALTER DATABASE My_Database RENAME TO dba;
ALTER DATABASE [My_Database] RENAME TO dba;
ALTER DATABASE 'My_Database' RENAME TO dba;
Then i tried below and its successful
ALTER DATABASE "My_Database" RENAME TO dba;

Why can I only see postgreSQL relations or tables when I'm logged in as the postgres user? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
postgresql database owner can't access database - “No relations found.”
The core problem: I have a Django website that's complaining about relations (tables) not existing when I connect to it after attempting to run a SQL script generated by pg_dump MY_DATABASE_NAME -cOx -E UTF8 > MY_SCRIPT_NAME.sql
Before running the script I'd could connect and all was right with the world.
After running the script I live in a world of pain that I've attempted to alleviate by examining and altering ownership of MY_DATABASE_NAME and tables using the various methods described in Modify OWNER on all tables simultaneously in PostgreSQL
It would seem that I can only see relations via \dt on MY_DATABASE_NAME only after having first logged in as the postgres user on my server like so: sudo su - postgres.
When I SSH and run psql MY_DATABASE_NAME -U MY_NON_POSTGRES_USER without switching to the postgres user, \dt' results in ano relations found` message.
At first I presumed permissions or ownership was the problem. I logged-in as my server's postgres user and changed owner of MY_DATABASE_NAME and all of its tables to MY_NON_POSTGRES_USER.
Same results as before. Relations show up for my postgres user, but logging in as another role results in a 'no relations found message', and my website complains that 'relation some_table doesn't exist'
Why is this happening to me?
Wow. I really just composed a TL;DR description of my question and realized immediately afterwards my folly:
Although I'd connected to MY_DATABASE_NAME and given ownership to the correct user for all tables and the database, I'd forgotten to:
REVOKE ALL ON SCHEMA public FROM MY_NON_POSTGRES_USER;
GRANT ALL ON SCHEMA public TO MY_NON_POSTGRES_USER;
No more pain. All is right with the world again.

rename database in psql

Could anybody help me to rename database in postgresql from the Linux shell
ALTER DATABASE name RENAME TO newname
The above statement doesn't execute
This may be a stupidly obvious question. Are you running psql as the postgres user?
e.g.
$ sudo -u postgres psql
# alter database FOO rename to BAR;
# \q
Which version of postgresql? From the 8.1 Documentation:
ALTER DATABASE name RENAME TO newname;
Only the database owner or a superuser
can rename a database; non-superuser
owners must also have the CREATEDB
privilege. The current database cannot
be renamed. (Connect to a different
database if you need to do that.)
You might need privileges to rename db. Only db owner or super user can do that, owner also needs a createdb privilege.
Also the database you're connected to cannot be renamed, you need to connect to a different one
You cannot rename a database you are connected to. Make sure you are disconnected before changing the dbname.
In PGAdmin, you can just right-click on the database itself, go to properties, and rename it from there.
As others have pointed out, you may also try the command :
ALTER DATABASE (DB NAME) RENAME TO (NEW DB NAME);
Disconnect database (Ctrl + F2 in DataGrip)
And then:
$ psql -U postgres
postgres=# ALTER DATABASE db_a RENAME TO db_b;
GL
Below given are the steps for renaming the database in postgresql.
1) Right click on database and choose refresh.
2) Right click again and choose properties option.
3) Under the properties tab, you can change the name with the one that you desire.