Restoring .bk file to postgreSQL database - postgresql

I was provided with a .bk file that I need to restore on a postgreSQL DB.
Reference for Restoring DB
I used the following command-line from the link above : psql -d EneFrame -U username -f backupfile.bk
After asking for the password, it seems to do some copying of rows, ALTER TABLE, CREATE INDEX and at the end REVOKE and GRANT - However, in the beginning I get the following errors, but still seems to run and finish... :
Then when I open pgAdmin - I dont see any change in the DB I am trying to restore to ?
I just started with postgreSQL today, so dont know much. Am I trying to restore incorrectly ?
Thanks for any help!

Related

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.

pgAgent won't launch after a database restore

I've installed pgAgent in our PostgreSQL database for scheduling our jobs, which really works like a charm!
However, after I restored a backup from our database in a test server, pgAgent simply won't launch. Interestingly enough, it seems that pgAgent ignores the current state of the data in this log tables and tries to populate them from zero.
See error message at the log file:
postgres#postgres ERROR: duplicate key value violates unique constraint "pga_jobsteplog_pkey"
postgres#postgres DETAIL: Key (jslid)=(1) already exists.
postgres#postgres STATEMENT: INSERT INTO pgagent.pga_jobsteplog(jslid, jsljlgid, jsljstid, jslstatus) SELECT 1, 25, 3, 'r' FROM pgagent.pga_jobstep WHERE jstid=3
In case you're wondering how the backup is performed:
pg_dumpall --file "/media/jones/Daten/fulldump.sql" --host "address-to-my-server.de" --port "5432" --username "myuser" --no-password --database "mydb" --clean --if-exists --verbose
Environment:
Ubuntu 16.04
PostgreSQL 9.5
pgAgent 3.4.1-2
Any ideas how to make pgAgent come back to life?
In case anybody is still looking, I've found what works in this situation..
Log onto the database, install the pgagent extension
Do the pg_restore of the pgagent schema
Then restore the pgagent.pga_jobsteplog after the rest of the schema has restored.
Then reset the value of the sequences for each table to be 1 above the max value of the column the sequence is based on
Doing this, means you pgagent working, and you also have you job and jobstep logs.
I'm definitely not happy with the solution, but so far I couldn't find anything better than the following options:
Truncating the log table does the trick, but deletes all job history you had (no big deal in most use cases):
TRUNCATE TABLE pgagent.pga_jobsteplog;
Alternatively, updating the sequences manually also works, e.g.:
SELECT SETVAL('pgagent.pga_exception_jexid_seq', max(jexid)) FROM pgagent.pga_exception;
SELECT SETVAL('pgagent.pga_job_jobid_seq', max(jobid)) FROM pgagent.pga_job;
SELECT SETVAL('pgagent.pga_jobclass_jclid_seq', max(jclid)) FROM pgagent.pga_jobclass;
SELECT SETVAL('pgagent.pga_joblog_jlgid_seq', max(jlgid)) FROM pgagent.pga_joblog;
SELECT SETVAL('pgagent.pga_jobstep_jstid_seq', max(jstid)) FROM pgagent.pga_jobstep;
SELECT SETVAL('pgagent.pga_jobsteplog_jslid_seq', max(jslid)) FROM pgagent.pga_jobsteplog;
SELECT SETVAL('pgagent.pga_schedule_jscid_seq', max(jscid)) FROM pgagent.pga_schedule;
If anyone has a more elegant solution, please let me know in the comments.

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.

psql: FATAL: database "Morne Nel" does not exist

I need to do a report in JasperReports.... besides the point here.
Ive been given a existing Postgresql DB to use.
I have installed PostgreSQL and all went great. (except the package update at the end. Cant get past the proxy server)
Ive added the C:\Program Files\PostgreSQL\9.0\bin path the the system path as-well.
When I go to cmd and type psql it propts for a password....
I enter password, because thats the only password I added during instalation.
Then is comes up with psql: FATAL: database "Morne Nel" does not exist
step by step hoe do I import this DB into postgres? PLEASE?
When using psql you have to pass database name, otherwise postgres will try to connect to database matching your user name (that is why it tries to connect to "Morne Nel").
You can use createdb console command to create database first. Here is documentation on that command: http://www.postgresql.org/docs/9.3/static/sql-createdatabase.html
But also you can use pg_restore command right away with --create option passed to create and restore database from dump at once (documentation here: http://www.postgresql.org/docs/9.3/static/app-pgrestore.html).
Try to execute those commands (it is possible that you will have to specify your postgres user and password) and let us know what happened. I cannot write more at the moment as I am not sure what dump format do you have so it would be good to provide that information if you won't manage to use pg_restore successfully.

pgAdmin III Restore PLAIN Backup

New to PostgreSQL database, I found column names were automatically converted to lowercase. I created a PLAIN backup, manually edited the column names to proper case. When I tried to restore the edited PLAIN backup, the OK button of pgAdmin3 Restore was disabled.
Is there a way run the edited plain backup file? Please help.
Use the client program psql to restore, works fine. The installers of pgAdmin and PostgreSQL also install psql, so you should have it on your pc or your database server.
psql -f backup.sql postgres