Copy table between databases - postgresql

Select * into exp.dbo.client from db.dbo.client
I'm trying to copy a table from one database to another in postgres and it's showing me that the references between the databases aren't implemented.
And if there an easier way to copy the whole database instead of table by table, please tell me.

After executing "create extension dblink" in the database you can after excute this query to copy tableOne from database to tableTwo, without creating it before
create table tableTwo
as
select *
from dblink('host=localhost
user=username
password=password
dbname=current_database',
'select * from tableOne') as linkable( var1 type1, var2 type2,var3 type3,... )

To clone the whole database on the same server:
CREATE DATABASE "db_new" WITH TEMPLATE "db_old" OWNER owner1

Related

How to transfer/push table from one database to another using dblink

Say we have 2 databases in postgresql: Database A(with dblink extension installed) and Database B (without dblink extension)
Copying table from B to A looks fairly straightforward:
select * into table_from_B from dblink('dbname=B', 'select id, name from some_table') as t(id bigint, name varchar(255));
What is the best way to copy/push a particular table from A to B using SQL?
Is it possible to do it using dblink without installing it on B ?

create (or copy) table schema using postgres_fdw or dblink

I have many tables in different databases and want to bring them to a database.
It seems like I have to create foreign table in the database (where I want to merge them all) with schemas of all the tables.
I am sure, there is a way to automate this (by the way, I am going to use psql command) but I do not know where to start.
what I have found so far is I can use
select * from information_schema.columns
where table_schema = 'public' and table_name = 'mytable'
I added more detail explanation.
I wanted to copy tables from another database
the tables have same column names and data type
using postgres_fdw, I needed to set up a field name and data type for each tables (the table names are also same)
then, I want to union the tables have same name all to have one single table.
for that, I am going to add prefix on table
for instance, mytable in db1, mytable in db2, mytable in db3 as in
db1_mytable, db2_mytable, db3_mytable in my local database.
Thanks to Albe's comment, I managed it and now I need to figure out doing 4th step using psql command.

Table I successfully created missing

I just had an RDS instance spun up to a Postgresql db. The database was created successfully and I am able to connect. Problem is when I run this code:
CREATE SCHEMA hollywood;
CREATE TABLE films (title text, release date, awards text[]);
SELECT * FROM hollywood.films;
This is the output I get:
Schema hollywood created
Table films created
An error occurred when executing the SQL command:
SELECT * FROM hollywood.films
ERROR: relation "hollywood.films" does not exist
What am I missing here? I added double-quotes around the schema name but to no avail. I opened up the permissions for the user thusly but to no avail (bad, I know)
grant all privileges on all tables in schema hollywood to bi;
I added the search path before my select statement thusly:
SET search_path TO hollywood; select....
No change.
Try:
CREATE SCHEMA hollywood;
CREATE TABLE hollywood.films (title text, release date, awards text[]);
SELECT * FROM hollywood.films;
or
CREATE SCHEMA hollywood;
SET search_path TO hollywood;
CREATE TABLE films (title text, release date, awards text[]);
SELECT * FROM films;

Copy data between two tables in PostgreSQL using dblink.sql

I am using PostgreSQL 9.1. I need to transfer required columns from one table of one database into another table of another database, but not schema.
I found that dblink.sql file has to be there in share/contrib. But my contrib folder is empty. Where can I download the dblink.sql file and can execute my query?
When I execute the query now it shows an error message:
cross database reference is not possible ...
Can anyone help me how to transfer the data between two databases?
After you have installed the package into your system as detailed in the related question install the extension dblink into your database (the one you are running this code in, the foreign db does not need it):
CREATE EXTENSION dblink;
You can find code examples in the manual.
Here is a simple version of what I use to copy data between dbs:
First, create a FOREIGN SERVER
CREATE SERVER mydb
FOREIGN DATA WRAPPER postgresql
OPTIONS (hostaddr '111.111.111.111',port '5432',dbname 'mydb');
FOREIGN DATA WRAPPER postgresql was pre-installed in my case.
Then create function that opens a connection, removes old data (opotional), fetches new data, runs ANALYZE and closes the connection:
CREATE OR REPLACE FUNCTION f_tbl_sync()
RETURNS text AS
$BODY$
SELECT dblink_connect('mydb'); -- USER MAPPING for postgres, PW in .pgpass
TRUNCATE tbl; -- optional
INSERT INTO tbl
SELECT * FROM dblink(
'SELECT tbl_id, x, y
FROM tbl
ORDER BY tbl_id')
AS b(
tbl_id int
,x int
,y int)
ANALYZE tbl;
SELECT dblink_disconnect();
$BODY$
LANGUAGE sql VOLATILE;

dump subset of table

I want to dump a subset of a table of my postgres database. Is there a way to dump a SELECT statement without creating a view?
I need to copy a part of the table to an other postgres database.
Use COPY to dump it directly to disk.
Example (from the fine manual) using a SELECT:
COPY
(SELECT * FROM country WHERE country_name LIKE 'A%')
TO '/usr1/proj/bray/sql/a_list_countries.copy';