How to set autocommit off permanently postgres - postgresql

I have a postgres instance with a postgres user.
root#f23ada822ac8:/# su - postgres
No directory, logging in with HOME=/
$ bash
postgres#f23ada822ac8:$ psql
psql (9.6.1)
Type "help" for help.
postgres=# \echo :AUTOCOMMIT
on
In / which is postgres home directory I have .psqlrc which looks like
postgres#f23ada822ac8:/$ cat ~/.psqlrc
\set AUTOCOMMIT off
Ideally this should have set autocommit off but it doesn't switch it off. I am new to postgres. Any pointers here would help. I know this is not permanent solution. I also read that even if auto commit is on if you have transactions then it would only get committed when you call commit no matter what the auto commit setting is. Is this true?

It depends on how you have interfaced with DB's in the past. Applications like TOAD have an auto-commit feature that can be turned off, and it has saved my bacon many times. That said.
psql has a start up file psqlrc. You can add the autocommit off option to this file. This will only work on psql however.

Related

How to clear PostgreSQL psql command history

When login to the database using psql -u postgres, all the commands entered there can be seen in and recalled from history.
I created a role with a password and I would like to clear that entry.
How can I clear the history?
Please check on the /home/user/.psql_history, then open the file if we want we can clear all the commands or the necessary commands and save the file.
Because you used -u postgres, the file should be in the home of your postgres user. In my case it is /var/lib/postgresql/.psql_history.

Export everything from PostgreSQL

I want to export everything(users, roles, object definitions, data) from my PostgreSQL and restore it into another server. How can I do that?
I try to use
pg_dumpall -U postgres -g > out.sql
but file out.sql is empty. Can someone explain me why this happens?
When I try:
pg_dumpall -U postgres > out.sql
PostgreSQL just didn't want to accept password for "postgres".
I prefer to do this job from (windows) command line(it is not problem and with linux command, I can translate them). And if there is nice pgAdmin way to do it, I will happy to learn it, too.
P.S: My password for "postgres" is correct (I try to login with "psql -U postgres" and there is no problem)
Update: I try with user different than "postgres"(in this case rsmn):
pg_dumpall -U rsmn > out.sql
I have following error:
pg_dumpall: query failed: ERROR: permission dneied for relation
pg_authid pg_dumpall: query was: SELECT oid, rolname, rolsuper,
rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolconnlimit,
rolpassword, rolvalidunil, rolreplication, rolbypassrls,
pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, rolname
= current_user as is_current_user FROM pg_authid WHERE rolname !~ '^pg_' ORDER BY 2
User privileges(This user is only one beside "postgres"):
The attempt with user rsmn fails because it is not a superuser and cannot export the user passwords.
It remains to figure out why pg_dumpall -U postgres fails for you.
For that, the first thing would be to check the PostgreSQL server log. I am not sure where that is on your system, you should check the settings in postgresql.conf (logging_collector, log_destination, log_directory, log_filename). Probably it is in the Windows Event Log.
The log message will tell you what is wrong.
It could well be a problem with the pg_hba.conf file – if so, modern PostgreSQL versions will tell you which line was used.
For debugging purposes, you can try to change the relevant line to trust authentication so that no password is requested (but don't leave it that way, or everybody can connect as user postgres without password!).
If you are trying to do it under the *nix and your server is installed localy, you may not to use ident mode instead of password:
sudo su postgres
pg_dumpall > out.sql

Creating a database dump doesn't seem to be working

I have PostgresQL 9.6.2 installed, and I'm trying to make a backup of one of my databases (my_database) using pg_dump command. It doesn't seem to be working as I see no output file.
I'm using a Mac and from my terminal (and in my project directory) I use:
psql postgres postgres
pg_dump my_database > my_database.bak
being postgres my database default user and password.
I've already tried using sudo psql postgres postgres with the same results.
I imagine that I'm experiencing some kind of lack of permissions but cannot understand it.
So, how to have the right permissions to do this?
Postgres comes with a bunch of stuff, including command-line tools. That includes the standard client, psql, and also the command pg_dump. That needs to be invoked from a shell command line.
If you type it into a psql command line, you'll get a syntax error once you terminate the line (with a ';') -- it's not valid sql, nor a valid command to psql.
Hope that helps!

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.

psql: FATAL: Peer authentication failed for user "expman"

I'm trying to restore a database from backup but I can't connect to postgresql.
namespace :db do
task import: :environment do
import_path = "~/backups"
sql_file = "PostgreSQL.sql"
database_config = Rails.configuration.database_configuration[Rails.env]
system "psql --username=#{database_config['username']} -no-password # {database_config['database']} < #{import_path}/#{sql_file}"
end
end
I tried changing the pg_hba.conf file (peer to md5).
In the console I tried the same thing with the super user postgres, but it still fails.
BTW, does anyone know a better way to restore a database? I used the backup gem.
EDIT:
I restarted the postgresql server and the passed the authentication. But, didn't restored the db. I reverted the changes in the file and just added -h localhost to the psql command. The database restores now. The only errors I get now are:
must be owner of extension plpgsql //and
no privileges could be revoked for "public"
after change pg_hba.conf, you shold reload or send a SIGHUP signal to postmaster pid. so that change applyed.
why not use psql -f to execute the backup sql file?
or you can use pg_dump backup and pg_restore restore. or copy command backup and restore.
LIKE :
digoal=# copy tbl_join_1 to '/home/pg93/tbl_join_1.dmp';
COPY 10
digoal=# delete from tbl_join_1;
DELETE 10
digoal=# copy tbl_join_1 from '/home/pg93/tbl_join_1.dmp';
COPY 10
OR
pg93#db-172-16-3-150-> pg_dump -f ./tbl_join_1.dmp -t tbl_join_1
pg93#db-172-16-3-150-> psql
psql (9.3.3)
Type "help" for help.
digoal=# drop table tbl_join_1;
DROP TABLE
digoal=# \q
pg93#db-172-16-3-150-> psql -f ./tbl_join_1.dmp
SET
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
ALTER TABLE