How to use psql to copy rows? - postgresql

I have 2 dbs on two different servers.
How can I copy using psql all the missing rows from db1 table to db2 table ?
If this is not possible.. How can I copy the entire table ?

Can you use a contrib module? If so, how about trying dblink. More information here

This is not possible with psql directly using a single SQL statement because you cannot connect to two different servers at the same time.
The only way you can do it:
connect to db1
export the table contents using psql's \copy command (if you have access to the server, you can also use the SQL statement COPY
connect to db1
import the text file using \copy or COPY depending on where the input file is located

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.

How to view pg_dump data with .sql extension?

I have downloaded pg_dump data from DeepDive Open Datasets. It is a file with extension .sql and to my knowledge that is a text file with SQL commands in it to recreate the database. I'm trying to peruse the data. How do I set up a Postgres database to do that?
I tried to use pgAdmin4 to view the data. However, I'm not sure how to set up and add a new server to read the data. I'm not sure if that is the correct approach.
I would appreciate any guidance with this.
You would view the dump itself with a pager like more or less; such a dump is a plain text file with SQL statements.
To restore such a dump you need to use psql, the command line client:
psql -U postgres -d adatabase -f dumpfile.sql
pgAdmin cannot restore such a dump, because it contains COPY ... FROM STDIN statements intermixed with the data.

How to move data from one database to another

I would like to copy data from table A database A to table b database b in postgres.
I am able to use copy to write data out to a file from table A in DB A
but when I try copy into database b table B it says "ERROR: must be super user to copy to and from file"
Please help and let me know how I should data from a flatfile into a table in a database.... the db is postgres 9.x
According to the documentation:
COPY naming a file or command is only allowed to database superusers or users who are granted one of the default roles pg_read_server_files, pg_write_server_files, or pg_execute_server_program, since it allows reading or writing any file or running a program that the server has privileges to access.
However you can use pipeline to transmit data from one database to another one without using of intermediate file(s):
psql -d A -c "copy a to stdout" | psql -d B -c "copy b from stdin"
Read more about psql utility and its usage.

Pgadmin 4 pg_restore: [archiver] input file appears to be a text format dump. Please use psql

Hi I am using Postgres 11 and pgadmin 4.1. I have a SQL file i am trying to import in my newly created database in pgadmin 4.
I know its a generalized error but I tried my best to resolve it, but not working for me.
Here is the error:
Here are a few SQL file lines.
There are two things in a plain text dump that may prevent it being restored using the query tool og pgAdmin:
backslash commands, like the \connect you get if you use the --create option of pg_dump (only psql understands these)
COPY commands to load the data, because pgAdmin does not support mixing SQL and data the way that psql does
So if the dump was created without --create and with either --schema-only or --inserts, it will probably load fine.

How to move/switch one database to another database using shell script(psql)?

How can I move or switch one database to another database using PostgreSQL 9.3 version in shell script(psql)?
Example:
testdb=#
to
mydb=#
If you mean in a script sourced by psql via -f or \i, you can use the command \connect (shorthand \c).
It doesn't just switch databases, it actually disconnects and reconnects. Any SET commands, etc, are not preserved across reconnection.
Simply use below statement:
\c database name