How to access postgresql DB server that has "too many clients" and kill connections? - sql

I was careless when I made some changes to my C# web service which accesses a postgresql database server at my work, and I forgot to close some connections I made. After testing the service many times over the past few days, I realized too late that I forgot to close my connections and now I can't access the server.
I've googled other solutions but they all involve me changing the max connection size in the postgres.conf file. The problem is that I don't have such a file on my own computer. I'm assuming the conf file is on the original machine that hosts the database server?
Is there any solution to this so that I can access the server again, and kill the old connections, without going to the original host machine of the database?

The only other options are:
to connect as the database superuser, which has superuser_reserved_connections other users cannot use; or
if you can, ssh in to the DB server and kill -TERM individual PostgreSQL worker backends. (on windows, that'd be rdp'ing in and using End Process on the backend postgres.exe processes, but it's much harder to pick a safe one to terminate on Windows, you'd want to use Process Explorer to examine the process heirachy and arguments so you can pick a regular backend).
In future you can prevent this by setting a connection limit on the user your appserver connects as; see ALTER USER.

Related

Locked out of Google Cloud server because of sshd.conf misconfiguration

While trying to solve another issue with connection problems to our servers, I thought to solve the problem by setting the MaxConnections and MaxStartups to my sshd.conf
When restarting ssh everything seemed fine, but this morning I found out that our Jenkins server didn't connect to any of the dev servers. So I tried logging into the system, finding out that I cannot log in to any of our dev servers anymore.
Looks like I made a F##$up in the sshd.conf and created a lockout for all the dev servers.
When trying to login I get an "port 22: Connection refused" error.
Is there any other way to get into the systems without having to connect every disk to another server to adjust the sshd.conf??
There are several options available for recovery in this situation:
Use the interactive serial console. This requires advanced preparation.
Add a startup script to fix the file, and then reboot to trigger the script.
Shutdown the instance, attach the disk to a recovery instance, use the recovery instance to mount the disk and fix the file, then detatch the disk and recreate the instance using the fixed disk. (you can also do this from a snapshot for added safety).

Is there an FTP server for testing error conditions?

One of the products I am maintaining has FTP uploads as part of its core functionality. We are encountering problems in production where the FTP server times out and the system doesn't react too well to that.
Is there an FTP server that I can use for testing scenarios like that? I want to be able to deliberately fail in different ways such as:
refuse connection (that one's easy)
stall until sockets time out
close connections in the middle of transmissions
Ideally it would let us configure behavior such as "drop the connection on every third upload, 300 bytes in".
I can see how to write this based e.g. on the Apache FTP server code, but I wonder if I am re-inventing a wheel. Is there an FTP server already that I can make fail in a controlled fashion?

Can the GUI of an RDP session remain active after disconnect

I'm running automated testing procedures that emulates keystrokes and mouseclicks 24/7.
Although it runs fine locally, on an RDP session it stops running once minimized or disconnected. Apparently, the GUI doesn't exist if you can't physically see it on the screen.
There is a registry work-around for keeping the GUI active for minimizing the window, but I know of no way to keep it alive after disconnect.
Ideally, I would have this run on the server Windows console session which would not care about being disconnected but in a hosted environment (I tried Amazon and Go Daddy) there is no way to access the console session.
Does anyone know how I can get around this? Basically any solution that allows me to run my application on a VPS. I need the reliability of a host but the flexibility to run it as if I was sitting right in front.
Yes, you can.
There are two types of sessions in Windows: The "console" session which is always active, and there can only be a max of one of, and "terminal" sessions, a la RDP. Using "rdpwrap" on Github, you can have an unlimited number of terminal sessions.
RDP sessions will become "deactivated" when there is not a connection to them. Programs will still run, but anything that depends on GUI interaction will break badly.
Luckily, we may "convert" a terminal session into a console session instead of disconnecting from Remote Desktop normally by running the following command from inside the terminal session:
for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (tscon.exe %%s /dest:console)
This will disconnect you from the session, but it will still run with full graphical context. This answers your question. You can reconnect to it and it will become a terminal session again, and you can do this infinitely. And, of course, autohotkey works perfectly.
But, what if you need more than one persistent, graphics-enabled session?
To get an unlimited amount of graphics-persistent sessions, you can run Remote Desktop and start terminal sessions from within the "main" session described above. Normally Remote Desktop prevents this "loopback" behavior, but if you specify "127.0.0.2" for the destination, you will be able to start a terminal session with any number of the users on the remote machine.
The graphics-persistentness will only be present on terminal servers if they are not minimized, unless you create and set RemoteDesktop_SuppressWhenMinimized to 2 at the following registry location:
HKEY_LOCAL_MACHINE\Software\Microsoft\Terminal Server Client
With this you can get an unlimited number of completely independent graphics-persistent remote sessions from a single machine.
This could be a workaround, altough I have not tried it myself and it involves having another machine
Let's assume that at the moment you are creating a session to myserver.com
Local Client ----> myserver.com
Instead of doing that, you could try having a separate server (let's call it myslave.com) and use that to establish a session
Local Client ----> myslave.com ----> myserver.com
Then if you disconnect the Local Client ---> myslave.com session the GUI of the session between myslave.com ----> myserver.com should remain active.
It will work only if you are connected to the console session of myslave.com.
I found a similar way. I had same problem, i downloaded rdp wraper which allows you configure multiple session rpd server and one tool which is included (rdpchecker.exe) allows you connect to localhost so you can connect to your server from your server and you dont need that middle client.
This could be a workaround, altough I have not tried it myself and it involves having >another machine
Let's assume that at the moment you are creating a session to myserver.com
Local Client ----> myserver.com
Instead of doing that, you could try having a separate server (let's call it myslave.com) and use that to establish a session
Local Client ----> myslave.com ----> myserver.com
Then if you disconnect the Local Client ---> myslave.com session the GUI of the session
between myslave.com ----> myserver.com should remain active
If you are using a windows server you don't even need another machine.
1) Connect to the server with the remote desktop connection (#con1).
2) Create a new alias for your server system like "127.0.0.2" in Windows\System32\drivers\etc\hosts .
3) Now establish a new remote desktop connection from your windows server (in #con1) to itself (#con2).
4) Finally start your GUI needing application e.g. UI-Path in #con2 and then close #con1.
I ran into the same problem and noticed that using VNC (TightVNC) to take over the remote machine seems to solve the issue. I guess VNC uses the console screen. Once activated and logged-in it stays logged-in, also after a VNC disconnect. Make sure that the screen never turns off in the power options.
Take note that keeping the console logged-in on a VPS is in general not recommended.

Checking server status on multiple machines from C# application

I have multiple content servers on different machines. I need to check the status of every server. I'm concerned about things like disk size, priority etc.
One solution that I'm using now is to install a Window Service on each machine which regularly checks the server but I have to manually install the service on each server.
Is there any way I can get the server information like disk space from a WCF service or using a windows application? I want to create a single watcher for my servers as I have to add servers sometimes.
Look at windows WMI you can make remote calls to machines so long as you have permissions to do so. You will only have to run one service on one server that can connect to the others.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394582(v=vs.85).aspx

Keep RDP Session Alive After Disconnect

Maybe there is a better way. Here is what I want to do.
I connect and download quotes all day and night using Web Server / Browser Based Application and SQL Database. How can I move this session to my web server so I do not have to use local resources to manage this process? When I connect to my Web Server using RDP, it will, of course, log me off all applications (including web browser) when I exit.
Thank for help and advice.
Craig
With RDP you should not be logged off. The session should remain even if you close RDP. But if you want to perform automatic repetitive tasks on a server, you should look into cron jobs, services or daemons, instead of relying on a user session with a running application.