Listing tables from imported database in PostgreSQL - postgresql

I want to display the tables from the database that I have imported into the PostgreSQL database. I tried using \d but it shows no relations found.
The size of the database is around 2 GB.
Is there any other option to list the tables in the database?
Here is the set of statements I executed to import the database and to list the tables in the database
postgres=# create database projects;
CREATE DATABASE
postgres=# \c projects;
You are now connected to database "projects" as user "postgres".
projects=# psql -d projects -u postgres -f demo.sql
projects-# \c projects;
You are now connected to database "projects" as user "postgres".
projects-# \d
No relations found.
projects-#

psql is a command line program, not a SQL command.
Do not enter it at the psql prompt.
If you properly end every statement with a ; the line psql -d projects -u postgres -f demo.sql would have given you an error right away.
To run a script from inside psql use \i demo.sql

Related

How to transfer data from one schema to another schema of different machine in Postgres db using Dbeaver

I have a Postgres database schema in machine A, I want to copy the whole database schema into machine B using Dbeaver. How can I do that?
You should take the backup using pg_dump. To take a dump from a remote server you should run the following
pg_dump -h host_address -U username -s schema_name -Fc database_name > dump_file_path.sql
This will create a SQL dump of the selected schema. Then you can use PSQL or DBeaver to import and create the schema in your server B or database B.
To do it with psql you should run the following -
psql "sslmode=disable dbname=_db_name_ user=_user_ hostaddr=_host_" < exported_sql_file_path

how to take backup of specific tables of a schema and restore on other schema

Suppose i have two databases in postgres
DB1
schema_abc
schema_xyz
...........
DB2
schema_lol
schema_qwx
Now i want to take backup of specific tables of schema_abc and restore them on schema_lol.
I am using pg_dump -U postgres DB1 --schema=schema_abc -t tbl1 -t tbl4 > file_name.sql
but search_path will be schema_abc in sql file, how can i restore this sql file to differently named schema of different db i.e schema_lol of DB2

List all databases in PostgreSQL when there isn't a db with same name of the current user's

First of all, I am new to PostgreSQL.
So am I right thinking that one cannot run most of the psql util commands nor non-db-specified sql commands if there isn't a db with same name of the current user's?
That is saying e.g., if I run psql "show databases;" as user postgres while there isn't a db called "postgres", I won't be able to run the command.
Question is that in this case, one cannot find the list of the dbs before knowing any of db exits, is that how it works?
You have to connect to a database. By default, the databases "template1" and "postgres" will exist and will accept connections.
If your PostgreSQL admin has changed things in such a way that you can't connect to either of those databases, you'll have to do one of two things.
Ask the PostgreSQL admin what database you're supposed to connect to.
Create a database, then connect to it. There's more than one way to do this.
If you have CREATEDB privileges, you can create a database on the psql command line. For example, I have CREATEDB privileges here, so I can do this, which creates the database "mike" and exits.
$ psql -h localhost -p 5435 -U mike -c "create database mike"
Now I can connect to "mike" by either taking advantage of the default database name, or by specifying it.
$ psql -h localhost -p 5435 -U mike
$ psql -h localhost -p 5435 -U mike mike
You can. If you connect (with proper user, usually postgres) to the postgres database there are several tables on the pg_catalog (PostgreSQL) among those is pg_database table a simple select * from pg_database will show all databases.
Here is an image showing that on pgAdmin III Tool
There is no way of doing exactly what you want without, at least, knowing the database catalog. The postgres database is default and will exist in all installed instances (unless someone had droped it). All RDBMs is the same they all have the catalog (also named information_schema, or other names depending on vendor) which holds all information about the databases, tables, constraints, etc.

Restore Database from dump.sql

I am getting this error while restoring data from the dump file.
nishant#nishant-Lenovo-G50-70:~/Documents$ psql sortation_gor1 < dump.sql
psql: FATAL: role "nishant" does not exist
I have followed the Postgress Ubuntu Documentation
But when I am trying to restore the database I am getting this error.
Any IDea. ?
PostgreSQL pg_dump doesn't save a roles. Roles in PostgreSQL are related to database cluster, not to single database. It does pg_dumpall with option -r. You should to create missing roles manually with SQL statement CREATE ROLE name LOGIN or you have to use export roles with pg_dump -r.
The I did it with the psql -U postgres -d d1atabase_name -f dump.sql

Moving a database with pg_dump and psql -U postgres db_name < ... results in "ERROR: relation "table_name" does not exist"

I moved my PostgresQL database from one hard drive to another using
pg_dump -U postgres db_name > db_name.dump
and then
psql -U postgres db_name < db_name.dump
I created the database db_name the same way in both instances. In the new database when I run my Java program with a JPA query (or a JDBC query) I get this error:
"ERROR: relation "table1" does not exist"
The query is:
select count(0) from table1
I know I've got a connection because if I change the password in the connection parameters I get an error.
For some reason in the new PostgresQL instance it thinks that table1 does not exist in the imported schema.
If I change the query to
select count(0) from myschema.table1
Then it complains about permissions:
"ERROR: permission denied for schema myschema"
Why would the permissions be different?
The table table1 exists in myschema because I can see it in the pgAdmin tool. All the rows were imported into the new PostgresQL instance.
When I do a query from Java the combination of pg_dump and psql created a problem.
What do I need to do to solve this issue?
Are you moving to the same version of PostgreSQL? There might be issues if you make a dump with pg_dump 8.3 and try to restore it in Postgresql 8.4. Anyway, assuming that it is the same version try the following:
Dump all global objects, such as users and groups (don't know if they were missing in your dump):
pg_dumpall -g -U postgres > globals.sql
Dump schema of database:
pg_dump -Fp -s -v -f db-schema.sql -U postgres dbname
Dump contents of database:
pg_dump -Fc -v -f full.dump -U postgres dbname
Now restore.
psql -f globals.sql
psql -f db-schema.sql dbname
pg_restore -a -d dbname -Fc full.dump
That is my $0.02. Hope it helps.
I encountered this problem. Then I realized that I forgot to install postgis extension.
Don't forget to install the extensions you use.
I was able to solve it by changing the database privileges to public CONNECT and the schema privileges for public and postgres = USAGE and CREATE.
My backup scripts apparently didn't preserve the privileges, at least not when moving from 8.3 to 8.4.