postgreSQL backup tables and views - postgresql

I want to have backup of my database, but using pgadmin III I can only restore tables, but I want my views be restored as well. Is there any way doing that?
tnx

you could do this task with CLI, eg:
-bash-4.2$ pg_dump -s $(psql -c "select string_agg('-t '||relname,' ') from pg_class where relkind='v' and relnamespace='public'::regnamespace" -At) | grep -i create
CREATE VIEW avva AS
CREATE VIEW v AS
Of course without grep to get the full definition.
otherwise you have to repeat for every view
or create in schema backup, DumpOptions #1 choose Only Schema...

Related

How I can create mocked views for my database that app has no migration scripts for it?

I have the following architecture of my application:
As you can see App1 is able to migrate Db1 whilst App2 is able to migrate Db2. App1 accesses only the views my_view1 and my_view2 from Db2.
Upon unit testing at App1 how I can create "fake" views with mock data that represent the actual views my_view1 and my_view2.
In order to make test views you'll need to generate an initial SQL script, that will generate a table similar to the view and the view will be just a select * from the table we generated:
psql -U your_user your_database -c "CREATE TABLE my_view1_mock as select * FROM my_view_1";
pg_dump -U your_user your_database -t my_view1_mock --schema-only > mock_views.sql
echo "CREATE OR REPLACE view my_view_1 as SELECT * FROM my_view1_mock" >> mock_views.sql
psql -U your_user your_database -c "CREATE TABLE my_view2_mock as select * FROM my_view_2";
pg_dump -U your_user your_database -t my_view2_mock --schema-only >> mock_views.sql
echo "CREATE OR REPLACE view my_view_2 as SELECT * FROM my_view2_mock" >> mock_views.sql
In another words we make a script mock_views.sql that will create the fake views. The mock_views.sql will be executed from unit-testing (such as phpunit) tool before running the actual test at tool's execution runtime. Therefore, empty test databases will need to be initialized as well for both Db1 and Db2.
The test data need to be populated in tables my_view2_mock and my_view1_mock in order to be shown in the actual views. Keep in mind this approach hides any logic/data population from the actual tables that populate the views.
In case of phpunit used the reccomended place to execute the sql script is at setUp method.

How to restore output from pg_dump into a new table name

I am dumping a large Postgres table like this:
pg_dump -h myserver -U mt_user --table=mytable -Fc -Z 9 --file mytable.dump mydb
The above creates a mytable.dump file. I now want to restore this dump into a new table called mytable_restored.
How can I use the pg_restore command to do this?
There is no pg_restore option to rename tables.
I would do it like this:
-- create a table that is defined like the original
CREATE TABLE mytable_restored (LIKE mytable INCLUDING ALL);
-- copy the table contents
COPY mytable TO '/tmp/mytable.dmp';
COPY mytable_restored FROM '/tmp/mytable.dmp';
you can append sed to your pg_dump so that while pg_dump outputs the table name, sed will replace it with the right one.
pg_dump mytable mydb | sed 's/mytable/mytable_restored/g' > mytable_restored.dump
My answer may not the exact answer for the question but for my case (directory dump with many tables), I have improved Laurenz's answer, it may be useful.
In my case I have just needed two tables to restore to another location.
First I created the tables in new location.
I find the table data in the toc.dat file.
pg_restore -Fd <dir_name> -l | grep <table_name>
This will probably return more than single line. Then we look for "TABLE DATA" line. After getting [file_number].dat.gz file and use it in copy command like below in psql.
COPY <table_name> from PROGRAM 'zcat /<path>/<file_number>.dat.gz'

Restore multiple tables postgresql pg_dump

I have a database with several tables and I want to add the data and structure of a selection of them to a different database containing already different tables.
I have created the dump file in the following way:
"C:\Program Files\PostgreSQL\9.1\bin\pg_dump.exe" -U postgres -w DBName -t table1 -t Table2 -t Table3 > "E:\Dump.sql"
This works fine and I have E:\Dump.sql with the dump of the three tables in it.
I have tried to make a restore with the following code:
"C:\Program Files\PostgreSQL\9.6\bin\pg_dump.exe" -C -U User -t table1 -t Table2 -t Table4 destdb < Dump.sql
but I get the error
no matching tables were found
.
Where am I doing wrong?
https://www.postgresql.org/docs/current/static/backup-dump.html
pg_dump is meant for **taking* backups, next section of manual is for restoring a backup:
Text files created by pg_dump are intended to be read in by the psql
program. The general command form to restore a dump is
psql dbname < infile
Also you mention three tables, two of which are mixed case - in order for it to work, you have to double quote it I believe. But anyway - restoring for you file would be:
psql YOURDBNAME -f E:\Dump.sql

Postgres: Copying particular tables from remote db, interrelated with foreign key relationships

I have a Postgres RDS instance for one of my apps.
I need to copy 3 tables from it to a similar clone of the database.
I see mytable_id_seq tables also, which now I know are called sequences in postgres terminology.
When I created a dump of those three tables, and restore them, do I have to do anything with the _id_seq sequences ?
Do I have to restore them too, for the dump data to work as it did in the original table?
When you restore the entire database from a dump, it contains CREATE SEQUENCE statements by default. These statements initialize sequences to the proper state. But if you make a partial dump, with only selected tables, you must set sequences manualy.
Assuming, that your table's name is "clip", you can check the current value using this query:
SELECT last_value FROM clip_id_seq
And if you want to update the sequence after restore, you can do it with this simple query:
SELECT SETVAL('clip_id_seq', SELECT MAX(id) FROM clip)
pg_dump -d database_name -t mg_cnd -F c > database_backup.sql
pg_restore -U database_user --data-only -d database_name -t mg_cnd -F c <file_location>
-d = database name
-t = table name
-F = Format
c = plain
-U = User
--data-only = transfer only table data
pg_dump documentation
pg_restore documentation

PostgreSQL: How do I backup database with name A and load it to database with name B?

I have two databases on the same server. One named A and one named B. Booth databases have the same structure. I want to empty database B and load it with data from database A. Which is the best way to do this?
I have tried to take backup of database A in plain format. Then open the resulting sql-file and replace every occurence of 'A' with 'B' and then run the sql-script. This worked but I think it should be an easier way to move data from one database to another. Is it?
I use 'pgAdmin III' as my tool, but this is not necessary.
This is my first post here, hope the question is relevant and structured well enough. I tried google first but found it hard to find anyone with the same question.
Thanks in advance!
/David
SOLUTION: After help from Craig, this is how I did it
pg_dump -Fc -a -f a.dbbackup A
psql -c 'TRUNCATE table1, table2, ..., tableX CASCADE'
pg_restore dblive.backup -d B -c (not sure if -c was necessary)
Backup:
pg_dump -Fc -f a.dbbackup
Restore:
psql -c 'CREATE DATABASE b;'
pg_restore --dbname b a.dbbackup
Use the -U, -h etc options as required to connect to the correct host as the correct user with permissions to dump, create and restore the DB. See the docs for psql, pg_dump and pg_restore for more info (they all take the same options for connection control).