PostgreSQL Cluster Startup Race Condition? - linux

My software process depends on the existence of a specific database in the local PostgreSQL cluster. PostgreSQL and my software process are both started by upstart on system start. A script is executed after PostgreSQL starts and before my software process starts to ensure the database is in the correct state.
The script dumps the schema of the database using pg_dump. If the server is not running or ready for connections I see the following
$ pg_dump -U postgres -s testdb
pg_dump: [archiver (db)] connection to database "testdb" failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
If the database does not exist I see the following
$ pg_dump -U postgres -s testdb
pg_dump: [archiver (db)] connection to database "testdb" failed: FATAL: database "testdb" does not exist
There appears to be a time window where even though the cluster does contain database 'testdb' the call to pg_dump reports that the database does not exist. I can avoid the problem by adding a short sleep between server startup and running my script. I would prefer a solution that does not include a sleep for an arbitrary length of time. Any suggestions?
I'm running PostgreSQL 9.1 on 32bit Ubuntu 12.04 LTS.

Assuming this is a shell script, my guess is that you are not waiting for the create database command to complete. You could be running createdb n the background or psql in the background. If you run these in the foreground this should not happen because they will block until the database completes.

Related

Starting MongoDB Replica Set

So, I was making an app that needs to use MongoDB transactions. But the Mongoose documentation told me that "MongoDB currently only supports transactions on replica sets, not standalone servers." So I thought I basically need to switch my Standalone MongoDB instance to Replica Set (whatever that means).
The MongoDB documentation gave me the instruction of how to do this with a few steps:
Shutdown the Standalone MongoDB instance
Restart the instance using the --replSet option
mongod --port 27017 --dbpath /var/lib/mongodb --replSet rs0 --bind_ip localhost
Connect the Mongo Shell
Call rs.initiate() inside the Shell
I'm stuck at step 2. All I know, when you want to start using MongoDB, you have to start its daemon first, using sudo systemctl start mongod, and then start using it by connecting your app. but that step told me to use mongod command to start the Mongod daemon, instead of systemctl. I tried the command but got the following error:
DBException in initAndListen, terminating","attr":{"error":"IllegalOperation: Attempted to create a lock file on a read-only directory: /var/lib/mongodb"}}
At first, I thought it was some kind of a privilege issue, so I ran it again with sudo but then it ended up destroying my entire database and prevented me from starting the MongoDB the "normal way" with giving me errors that I cannot remember.
I just reinstalled the whole MongoDB to get it back to work fine. Now I'm at the same place as yesterday, unable to convert to replica set, only now my entire database is gone. What do I do to enable it?
When running the mongod as a service, use /etc/monogd.conf to set the configuration. Note that the location or name of this file might have been changed in the mongod.service file in your system.
See replication options for how to set that in the file.

Postgres - Moving Data Folder

I wanted to move my PostgreSQL data folder on my Ubuntu server and I was following this tutorial to move it.
I was able to move it, and update my conf file to point to the new location.
When I go to run Postgres, it starts, but when I try to connect via
sudo -u postgres psql
It gives me this error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Any ideas what happened? It says it's running.
After done moving PostgreSQL data directory. Don't forget to restart the PostgreSQL service.
Check the PostgreSQL status by command ps -ef | grep postgres. -D parameter value should be new directory location.
ERROR /var/run/postgresql/.s.PGSQL.5432 that's mean your PostgreSQL service not starting up Properly or Failed to when starting up.

Mongo shell not connecting

Mongo shell not connecting
I have dedicated centos server
I installed mongodb as a service in my server from here
I even restarted server after installing mongodb
When i tries to run mongo shell it always saying
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
I tried to remove that mongod.lock but no joy
I dont understand why this happens, because when mongod starts as a service it creates a log in system and telling that it has opened the door to listen incoming connection on port 27017. But it is throwing error while i connecting to mongo shell.
Please see this steps i have used --> click here
Well i wants to use this mongo service first of all at local based with only 127.0.0.1 access. I can change that in mongod.conf file afterward.
Be sure with iptables, Firwalll causing this kind of issue on live server.
So please do this if you facing this kind of same issue,
Do iptables off for a moment
# /etc/init.d/iptables stop
# sudo service mongod start
# mongo
Here what i wanted to see finally,
MongoDB shell version: 2.6.7
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
show dbs
admin (empty)
local 0.078GB
Thanks guys who considered my question and looking in it.
Thanks a lot.

MongoDB stuck on clientcursormon (waiting for connections on port 27017)

After simply installing MongoDB and creating \data\db within the MongoDB folder, I proceeded to run mongod.exe --dbpath C:\MongoDB\data\db. It runs a bunch of initandlisten code and then when it waits for connections on port 27017 it hangs and writes out mem res:47 virt:171 mapped:80 connections:0. I am very new to Mongo and haven't found a definitive answer on why this is hanging and how to fix it.
If you want to run the mongod.exe process as a service under Windows (to avoid it taking up a command prompt) you should following these directions for MongoDB 2.6.0 (2.6.0 has a bug with the normal approach to configuring MongoDB as a service).
There is a known issue for MongoDB 2.6.0, SERVER-13515, which prevents
the use of the instructions in this section. For MongoDB 2.6.0, use
Manually Create a Windows Service for MongoDB to create a Windows
Service for MongoDB instead.
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#manually-create-windows-service
For MongoDB versions 2.6.1 and later follow the normal directions:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb
Mongo waiting on 27017 even after reinstall
The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.
After you install MongoDB, you need to connect to MongoDB through mongo.exe shell using another command prompt.
As soon as you are done with that, the waiting message in first console would change to connection accepted.

postgresql commands not being executed

I downloaded postgresql-9.1.
but when I execute the command in terminal:
$ createdb mydb
I get the following message:
createdb: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
please tell me how to sort this problem.I have even tried to uninstall and then reinstall it. I am trying to run this on linux mint 15 (cinnamon).
Use ps to see if the postgresql process is actually running. If not, start it (service postgresql start). Normally should start up itself on boot after installation but who knows. Also, some error may prevent it from starting on boot.
If postgresql is running, then you may need to edit pg_hba.conf in the data directory. Again, normally the database should listen for local connections by default but who knows.

Resources