Friday, June 7, 2013

Enabling remote access to a PostgreSQL database hosted on AWS

The goal: To enable access via pgAdmin and psql from our local machine to a PostgreSQL database server hosted on an Amazon Web Services EC2 instance.

The environment:
Server:
  • AWS EC2 Instance running Ubuntu Server 12.04.1 LTS, 64-bit
  • PostgreSQL 9.1
  • Default, out-of-the-box PostgreSQL configuration. That includes listening on port 5432, a database named postgres, and a role named postgres.
Client:
  • Windows 7
  • pgAdmin III version 1.16.1
  • psql version 9.2.3
Until the proper security settings are applied, the client can't connect to PostgreSQL on the server. If we try to connect using pgAdmin, a Server not listening error occurs:

If we try to connect using psql, a Connection timed out error occurs:

To enable connections, we must edit the AWS Security Group and two PostgreSQL configuration files.

Editing the AWS Security Group:
  • Log in to AWS.
  • On the EC2 Dashboard, select your Instance and note which Security Group it's using:
  • Select that Security Group, click the Inbound tab, and add a rule. The port should be 5432, and the source should be the IP address or our local machine (not the server), followed by /32. Don't forget to click the Apply Rule Changes button.
 
Editing the PostgreSQL pg_hba.conf file:
  • Use an SSH client to connect to the EC2 Instance.
  • cd to the directory that contains the PostgreSQL configuration files. This may vary, but mine are in /etc/postgresql/9.1/main.
  • Use a text editor to modify pg_hba.conf.
  • Locate the line host    all             all             127.0.0.1/0               md5.
  • Immediately below it, add this new line: host    all             all             0.0.0.0/0               md5
  • Save the file.
Editing the PostgreSQL postgresql.conf file:
  • Use a text editor to modify postgresql.conf.
  • Locate the line that starts with #listen_addresses = 'localhost'.
  • Uncomment the line by deleting the #, and change localhost to *.
  • The line should now look like this: listen_addresses = '*'                  # what IP address(es) to listen on;.
  • Save the file.
  • Restart PostgreSQL. The command may vary. In my case, it was: sudo /etc/init.d/postgresql restart.
Now you should be able to connect to the PostgreSQL server from your local machine, using your choice of pgAdmin3 or psql!
 
You'll be prompted for the postgres role's password when you connect, unless you've stored the password in %APPDATA%\postgresql\pgpass.conf on your local machine. (If your local machine is running Linux, the analogous file is .pgpass, usually found in your home directory.)
 
Caveats: The above instructions aren't optimized for security. In a production environment, you'll likely want tighter control over access to PostgreSQL. For example, in pg_hba.conf, we granted access to all IP addresses, which might be overkill. And logging in as the default role, postgres, might not be the best idea.
 

6 comments:

  1. thanks mate... Your blog helped me.

    ReplyDelete
  2. Defying Pyramid PostgreSQL Connection Issue? Contact to PostgreSQL Remote Database Service to settle it
    With the help of Postgres SQL Support for Linux or Postgres SQL Support for Windows you can without a lot of an extend screen the execution of your Postgres database and perceive if there is any issue. Well in case you are dumbfounding about your affiliation issue by then quickly make our medicinal move and resolve main problems even before it hits your end customers. Our PostgreSQL Relational Database Service can without a doubt track the execution of your entire Postgres condition.
    For More Info: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete
  3. Solve Postgres Database Encoding Problem with to Postgres SQL Support for Linux

    Rapidly investigate your Postgres database encoding issue with Cognegic's Postgres SQL Support for Windows or PostgreSQL Relational Database Service. The essential capacity of Postgres is to store information commonly however once this encoding issue is emerge then it progresses toward becoming to deal with your Postgres condition. To determine this issue we give helpful help which is under in your financial plan. In this way, specifically contact to our specialized specialists and investigate the propel bolster.

    For More Info: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete
  4. Investigate your Postgres SQL Issues with Postgres SQL Support for Linux

    Cognegic continually saw as best Postgres authorities who have long history of innovative Postgres database change and association. Our particular authorities in like manner give cutting edge support to our clients and resolve their request by giving top notch help. With proactive help and minute prepared framework we guaranteed unwind you're all issues. We can moreover help you in the course of action of proper checking structure. Beside Postgres Support we moreover give Postgres SQL Support to Windows or PostgreSQL Remote Database Service.

    For More Info: https://cognegicsystems.com/

    Contact Number: 1-800-450-8670

    Email Address-info@cognegicsystems.com

    Company's Address-507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete
  5. Not Able to Access PostgreSQL Database Remotely? PostgreSQL Remote Database Service
    The PgAdmin is a free and open source GUI which is wonderfully proper for a couple of stages including Windows, Mac, Linux and OS X. The PgAdmin similarly offers server information recuperation, testing, and nonstop upkeep. However, by far most of the customers don't know how to get to the PostgreSQL database remotely with PgAdmin. Contact to Cognegic's Postgres SQL Support for Windows and Postgres SQL Support for Linux for an authentic system of PostgreSQL database remotely with PgAdmin.
    For More Info: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete
  6. Not Able to Fix Your Connection_Failure Issue in Postgres? Contact to Postgres SQL Support for Windows
    Is your Postgres association comes up short? Or then again neither could nor associate with the server? On the off chance that yes, at that point doesn’t freeze, simply attempt these underneath ventures to explain your association disappointment issue in Postgres. In the initial step, you need to permit remote IP to deliver to get to PostgreSQL after that permit correspondence over TCP/IP at that point restart PostgreSQL server lastly test your setup. By along these lines, you can without much of a stretch investigate your association disappointment issue. Be that as it may, while physically tackle this issue in the event that you discover any trouble at that point quickly contact to PostgreSQL Remote Database Service or Postgres SQL Support for Linux.

    For More Info: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete