PostgreSQL 9.5 pg_hba.conf corrupt - postgresql

We have a PostgreSQL 9.5 installed on the system and sometimes the postgres service can't be started. When I look into the pg_log I see the following messages:
2017-02-07 12:55:18 IST [4800]: [3-1] user=,db=,app=,client= LOG: configuration file "C:/Program Files/PostgreSQL/9.5/data/pg_hba.conf" contains no entries
2017-02-07 12:55:18 IST [4800]: [4-1] user=,db=,app=,client= FATAL: could not load pg_hba.conf
After looking into the pg_hba.conf file I can see that the whole file is corrupt. When I open the file in the Windows editor I can't see anything except spaces and when I open it in Notepad++ there are only "NUL" characters in the whole file.
To fix this we copied a fresh version of the file to its location and after that the service starts without any problem. But how can this happen and what can we do to avoid it.

Related

Get databases from old PostgreSQL folder (win 10)

I need to get databases from the old PostgreSQL folder that was in Program Files (win 10).
I tried changing the data path through the registry (ImagePath
value after "-D" at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\postgresql-x64-12) from the current to the old PostgreSQL data folder.
I also tried replacing files in the current folder from PostgreSQL with old ones (replaced data for C:\Program Files\PostgreSQL\12\data\base and C:\Program Files\PostgreSQL\12\data\global).
In both cases, an error occurred when trying to start the postgresql-x64-12 service. Error message: the postgresql-x64-12 service on local computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.
How can I get databases from old ../Program Files/PostgreSQL folder?
After replacing the folders C:\Program Files\PostgreSQL\12\data\base and C:\Program Files\PostgreSQL\12\data\global with the old ones and attempts to start the postgresql-x64-12 service, a log file appeared with the following content:
LOG: database system was shut down at ...
LOG: invalid primary checkpoint record
PANIC: could not locate a valid checkpoint record
LOG: startup process (PID 17452) was terminated by exception 0xC0000409
HINT: See C include file "ntstatus.h" for a description of the hexadecimal value.
LOG: aborting startup due to startup process failure
LOG: database system is shut down
Because of the message PANIC: could not locate a valid checkpoint record, I used pg_resetwal: in PowerShell I opened the folder C:\Program Files\PostgreSQL\12\bin, then ran the command .\pg_resetwal.exe -f -D "C:\Program Files\PostgreSQL\12\data. In some cases, it is worth using the command without -f, also for other versions of Postgres a different command may be needed. More information: https://stackoverflow.com/a/8812934/14145682.
After that, the service also did not start, and the following error was present in the log:
FATAL: could not access status of transaction
DETAIL: Could not read from file "pg_multixact/members/0000" at offset 8192: read too few bytes.
So, I replaced the files in the C:\Program Files\PostgreSQL\12\data\pg_multixact folder with the old ones.
After these steps, the postgresql-x64-12 service started without errors and it was possible to restore the old database.

Can't edit postgresql.conf file to change settings

After editing postgresql.conf file and restarting a server it seems that all changes do not apply.
I was following this tutorial:
https://dba.stackexchange.com/questions/153904/pgadmin-4-server-status-view-log-file?newreg=9500cbcc4a794d2fbfd6ef5bb787e0e5
To create .csv log files for postgresql database. In PGAdmin 4 I've run
SELECT sourcefile FROM pg_settings
and
SHOW config_file
that showed me path of conf file: /etc/postgresql/10/main/postgresql.conf
I've located and edited that file on my Debian server through midnight commander and changed lines:
log_destination='csvlog'
logging_collector='on'
log_directory ='log'
log_filename='postgresql.log'
And then I've restarted the server
sudo service postgresql restart
And now I have problems. I can't find .csv logs that are supposed to be saved on a server in 'log' directory. There is no such directory in /etc/postgresql/10/main or var/lib/postgresql/10/main. The only logs I could have found were on var/log/posgresql, but there aren't any .csv files I was searching for. Also I'm not sure if any of my edits in postgresql.conf file are even working, because I checked the setting from pgAdmin and thats what I got:
SHOW log_destination
stderr
SHOW logging_collector
off
So it looks like settings from postgresql.conf are not properly applied. How to properly set .csv logs for postgresql and where they can be found?

Changing postgresql 9.3 database location on Amazon ec2 linux

I apologize for the long post. I have a Postgresql 9.3 server running on a Amazon linux AMI. I also have a compressed dump file from another server which I created using pg_dumpall. Now, I want to restore the data from this dump file in my Postgres. However, I want to load this data into a specific location (say /data).
I'm having a fresh installation of Postgres. So when I tried to do a:
sudo service postgresql93 start
I got an error message asking me to initialize the db. So I did a:
sudo service postgresql initdb
which created the required files in /var/lib/pgsql93/data. After that, I changed the 'data_directory' configuration in /var/lib/pgsql93/data/postgresql.conf and pointed it to /data (I had to do this as root user. I couldn't open the file as the default user).
Now when I try to do a
sudo service postgresql93 start
it fails to start, and when I check the /var/lib/pgsql93/pg_startup.log file, it says:
FATAL: "/data/postgresql" is not a valid data directory
DETAIL: File "/data/postgresql/PG_VERSION" is missing.
So I copied the files from the default (/var/lib/pgsql9.3/data) to /data, changed the permissions to 700 and owner to postgres.
However, when I try to start the service again, it still fails, and in the pgstartup.log, it only says:
LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".
And when I check the log in /data/pg_log, it says:
LOG: database system was shut down at 2014-12-30 21:31:18 UTC
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
What else could be the problem? I haven't restored the data yet. I just have the files which were created by the initdb command.
#BMW http://www.linuxquestions.org/questions/linux-server-73/change-postgresql-data-directory-649911/ is exactly what I was looking for. Thanks.

Postgresql and SSL certificat

I'm trying to install PostgreSQL with another data directory.
The problem comes when I try to start postgresql I have the following error :
Starting PostgreSQL 9.1 database server: main[....] The PostgreSQL server failed to start. Please check the log output: 2014-04-16 22:14:51 CEST FATAL: could not load server [FAILficate file "server.crt": No such file or directory ... failed!
failed!
I've tried a lot of things coming from here or here but nothing works for me.
My configuration file is in /etc/postgresql/9.1/main/ but I don't know why I have a some other configuration files on my new data directory. (visibly they are not used).
Naturally I can fix this bug by commenting the ssl = true on the postgresql.conf but it's not at all the aim.
According to the PostgreSQL Reference you should check your $PGDATA directory for the server.crt file. In cases is missing you should either request a new certificate or make self signed one and place the file there.

Postgresql: LOG: autovacuum launcher started is hung (apparently)

I am working on this Heroku tutorial and I have a question about autovacuum process. There is still no tables or data and autovacuum launcher is hung for over half an hour now. Below is the cmd window.
I found a similar question here but could not find the answer. Does anyone know what is going on?
As a summary: After the last line LOG: autovacuum launcher started the cursor is blinking under the last line but nothing is happenning for over half an hour.
C:\Users\a>initdb pg
The files belonging to this database system will be owned by user "a".
This user must also own the server process.
The database cluster will be initialized with locale "English_United States.1252
".
The default database encoding has accordingly been set to "WIN1252".
The default text search configuration will be set to "english".
Data page checksums are disabled.
initdb: directory "pg" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "pg" or run initdb
with an argument other than "pg".
C:\Users\a>postgres -D pg &
LOG: database system was interrupted; last known up at 2013-10-05 13:46:39 EDT
LOG: database system was not properly shut down; automatic recovery in progress
LOG: record with zero length at 0/17704F8
LOG: redo is not required
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
EDIT
As suggested in bma's comments, I removed the pg directory and the project directory and I did the same steps again and I got the same result: LOG: autovacuum launcher started and it hangs. What am I going wrong?
EDIT
I opened a new command window and started the repl there and I was able to execute
C:\Users\a\CLOJURE\shouter>lein repl
user=> (require '[clojure.java.jdbc :as sql])
nil
But the next command gave the following error:
user=> (sql/with-connection (System/getenv "DATABASE_URL")
#_=> (sql/create-table :testing [:data :text]))
user=> IllegalArgumentException db-spec null is missing a required parameter cl
ojure.java.jdbc/get-connection (jdbc.clj:192)
How can I fix this?
EDIT
It turned out that 'export' is for unix and in Windows I needed to use 'set'. See this related question.
What is happening is that you are starting postgres directly, with the pg directory, and seeing log output. It would be better to install it as a service on Windows, and use the default data directory. This is what the normal installer does.
However your current approach is going to just run the db until you close the terminal window at which point it will die (hence the recovery at the beginning). You don't have hung processes. That's just log output.