How to copy table from one database to another using postgresql - postgresql

I am trying to copy a table from one database to another database But no response in server
Here is the command
pg_dump -U database_user_name -t categories my_current_db_name | psql new_db_name
What wrong in the above code it is not showing any response.

If something does not work then divide it into smaller problems.
At first try to run:
pg_dump -U database_user_name -t categories my_current_db_name
What is it printing? Do you see error message or correct dump output?
Then try to run:
psql new_db_name
Is it able to connect to database?
Possible problems:
new_db_name does not exists
you cannot connect with source or destination database because of wrong user/password/db host/port

Related

How to create a table from backup file in Postgres?

After making some schema change in table message in postgresql 13 database, the table is backed up in pgadmin4 in a file named message.sql. The schema change needs to be populated in another database. What I did is to drop the table message in that database. But I have hard time to re-create table message from the backup file messages.sql (not using CREATE table message ....). I use pg_restore to restore data but was not successful for re-creating table schema. Here is command I tried with no luck:
pg_restore --data-only -h localhost -U postgres -W -d dynamo -t messages /home/download/messages.sql
pg_restore --clean -h localhost -U postgres -W -d dynamo -t messages /home/download/messages.sql
I also tried to create an blank table (no column) called messages in the database and repeated above command, again without luck.
Here is how a new table is added to the current database:
backup the current db on the server with pg_dump
create the new database on dev PC with addition of new table
On dev PC, use pgadmin to restore the backup file in step 1 to new database created in step 2.
backup in pgadmin and create a .backup file for db in step 3.
on server, use pg_restore to overwrite the current db with new database backup file created in step 4:
pg_restore --clean -U postgres --dbname=mydb -W -h localhost --verbose /home/download/mydb.backup
open psql terminal to verify the new table

Problem restoring databse between 2 RDS instances using pg_dump and pg_restore

I'm having difficulty restoring a DB to an AWS RDS Postgresql instance. Context is that i am backing up from one RDS instance and restoring to another RDS insurance. They both have the same version of Postgresql 9.6.5.
I was able to take a dump using the following command:
./pg_dump.exe -U dbuser -W -h prod-pgsql-rds.3ft5coqxjdnq.eu-west-2.rds.amazonaws.com -d devdb > c:\tmp\backup.sql
From the resulting .sql file, I then attempted a restore to another RDS instance which is also using Postgresql 9.6.5 using below command:
./pg_restore.exe -U dbuser -d testdevdb -h dev-pgsql-rds.cym8coqx52lq.eu-west-2.rds.amazonaws.com "c:\tmp\backup.sql"
*I also tried the -f switch in the above restore command instead of the " " quotes before/after the file name
But when I try to restore it to a newly created database I get the following error:
pg_restore: [archiver] input file does not appear to be a valid archive
Can anyone help? FYI, I am using PGAdmin 4 via Windows PowerShell. I have to edit some of the values in the strings above due to data sensitivity.
pg_restore is only used for the other, non-plain-text output formats that pg_dump can output. For .sql dumps, you just use psql. See the docs on restoring from backups.
In a Unix env, you'd do psql [yourflags] < /tmp/backup.sql, but I'm unfamiliar with powershell and don't know if it supports < for input redirection; hopefully either it's present or you know the equivalent PowerShell syntax.
So I couldn't get psql or pg_restore to work so opted to import the .SQL file into via the SQL query tool in PGAmdin. This through up some errors so had to make several changes to the .SQL file and perform below:
Commented out a couple of lines that were causing errors
Elevated permissions for the user and made him the owner of for the Schema and DB properties by right-clicking on these via PGAdmin
The .sql file was making several references to the user from the source RDS DB so had to do a find and replace with a user account created for the destination RDS DB. Alternatively, I could have just created a new user on the destination DB with the same username and password as the source DB and then make him the owner in ref to step 2.

How to check table value and export the database using PostgreSQL and Ubuntu

I need one help. I need to export one database from my postgreSQL which is running in my ubuntu server.
first i typed the below command
sudo -u postgres psql postgres
it gave me the following result.
I checked all database using below command
\list
It gave me the following result.
I needed to connect the 100salons_production database and i connected using the following command and checked the tables.
\connect 100salons_production
\dt
it gave the following result.
Now i tried to check the table values using this command select * from areas but it does not show any result.
Here I need to check the tables results.and also i need commands to export this database and import it in somewhere else. Please help me to resolve this issue.
If you only want to export the database, there is not need to connect to it using the psql tool. Instead, you have to use the pg_dump tool. You can do that like this:
sudo -u postgres pg_dump DATABASE_NAME > dump.sql
Then you should transfer dump.sql to the other computer and import it there.

Is there a way to repopulate with command-line a postgres db that I can't drop?

I'm looking to load a database from a backup.gz. The backup is raw sql generated from pg_dump -U postgres app_development -f backup.gz -Z9.
I've tried dropping the db with psql -Upostgres -c "drop database app_development" but I get:
ERROR: database "app_development" is being accessed by other users
DETAIL: There are 3 other sessions using the database.
The same thing happens when I use dropdb.
I don't want to dump to a non-ascii version so I don't think I can use pg_restore.
Also, I'm not sure if it helps, but all this is happening in docker.

postgres copy tables from another database using pg_dump?

I would like to copy two tables from database A to database B, in postgres
how can I do it using pg_dump without losing the previous tables and data in database B ?
I read some answers in Stack Overflow suggesting using pg_dump but in the documentation page I read?
The idea behind this dump method is to generate a text file with SQL
commands that, when fed back to the server, will recreate the database
in the same state as it was at the time of the dump
Doesn't that mean it will delete the previous data in database B?
If someone could tell me step by step solution to move two tables in database A to database B without losing any previous data in Database B, it would be helpful.
I found the answer to my question :
sudo -u OWNER_USER pg_dump -t users databasename1 | sudo -u OWNER_USER psql databasename2
if you pg_restore a database into b database, of course a will replace b. instead pick specific table you would like to restore using pg_restore -t
you could pg_restore to different schema, by using -O (no_owner)
so let say
pg_dump -Fc -f dump.dmp -v -h host -U user_login -n schema_to_dump
you can
pg_restore -v -h host -U user_login -n schema_to_import -a --disable-triggers dump.dmp