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

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.*

Related

How to DROP tables from specific database-schema in Postgresql?

I am new to Postgresql and so far I have not found a way to drop a table from specific database. To give some context:
We are doing a synchronization from Oracle to PostgreSQL of 5 tables. In postgres I have a database SoloCopy and the schema is the default public. In the Postgresql instance we have also 2 more databases SoloSynch and postgres (the default one).
What I want to do is to select SoloCopy database and:
DROP TABLE public.table1;
When I do the above DROP statement table1 is deleted only from the database that was selected when opening SQL Query. But I want to specify the database before that and to be irrelevant from where the SQL Query was open. How can I do that?
I found an answer on my own. Setup can be found here:
Psql in Task Scheduler does not run a file script
Basically I needed to use PSQL and with the connection string there I connect to a specific DB, which I can drop the tables from. The details for PGPASSWORD and the .bat file I ended up creating are in the link above. Had to start everything from CMD and switch to psql from there.

Non-SuperUser Database Management

I am using the PostgreSql manual and other guides to setup my database environment.
So far things are looking good, but I am wondering if I have a problem.
I am trying to apply this:
Tip It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.
I have already created a user for this role.
createuser --interactive --pwprompt
Enter name of role to add: Manager
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
...and connected to an already existing database as the new user (Manager) Or so I thought.
psql -d test
Password for user postgres:
psql (11.2)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
test=# \c test Manager
Password for user Manager
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
You are now connected to database "test" as user "Manager".
test=>
I created a table for the database "test", but on checking it,the Owner isn't Manager, but postgres.
test=> \dt
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+----------
public | Data-projects | table | postgres
(1 row)
test=> \dn
List of schemas
Name | Owner
--------+----------
public | postgres
(1 row)
I am not sure if this is good. I expected the owner to be Manager.
This is my first time using these tools. Can someone guide me in the right direction, please?
I want Manager to manage all the databases.
Wouldn't that make him owner?
Thanks
Database test is not owned by Manager because it was created by user postgres. The user who created an object will be its owner.
However, a superuser can transfer ownership:
ALTER DATABASE test OWNED BY "Manager";
I'll give you some additional tips:
Only use lower case letters, numbers and _ in the names of objects and users, because otherwise you always have to user double quotes for them in SQL, like I did above. That leads to needless suffering.
Run the following to make the database more secure:
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
Create a new schema that will hold the objects for your application:
CREATE SCHEMA myapp;
Set the search_path appropriately:
ALTER DATABASE test SET search_path = myapp, public;

What happens after a "DROP DATABASE postgres"

I have a funny question about PostgreSQL database: What happens if the postgres database is dropped?
dropdb postgres worked.
createdb postgres worked too.
psql worked.
But I thought the users would be lost. Yet the old users are still there.
So where are the users stored for the database and which implications does dropping the postgres database have?
PostgreSQL metadata are stored in catalog tables, which are in the pg_catalog schema. These are accessible like regular views and tables.
There are shared system catalog tables which are shared between all databases. These tables are not affected when databases are dropped.
pg_authid, the table where the users are stored, is one of those shared catalogs. This is because in PostgreSQL, users don't belong to a database, but to the whole database cluster.
You can list all shared catalog tables like this:
SELECT relname FROM pg_class
WHERE relisshared AND relkind = 'r';
In the documentation you can find more information about the system catalogs.
When connecting to a Postgres server, you always need to specify which database you want to connect to.
When you set up a new server, you need something to connect to before you can run your first CREATE DATABASE statement.
That's all the postgres database is: a dummy database to use as a connection target for admin commands. There's no data in there, and you're free to drop it and use a different one instead (though whoever inherits your system will probably not thank you for it...).
As gil.fernandes said in his answer, server-wide objects like users are accessible from every database, but aren't stored inside any database in particular.

Why command \dt gives - no relations found?

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!

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.