Forums

Working with Postgres

I just upgraded my account for Postgres support but can't seem to figure out how to make it work correctly. I'm following a tutorial with the following steps:

First you need to start the Postgres server. Remember this command - any time you get connection errors talking about port 5432, run this again:

$ sudo service postgresql start

The next step is to create a user which matches your default user. To do this you can use the createuser command.

$ sudo sudo -u postgres createuser ubuntu --interactive

Each user needs a database associated with it, so create that now:

$ createdb ubuntu

If all has gone well, you can simply type the psql command and get directly into the postgres command prompt.

$ psql
psql (9.3.7)
Type "help" for help.

ubuntu=#

There are many problems with this, such as not being able to "sudo" commands. However, even when using the Postgres console, I still can't connect to any database, either the default one or the new "ubuntu" DB, as I receive the following error:

$ psql -p 10494 --username=ubuntu
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.10494"?

If I run my Python code, I get a similar error:

psycopg2.OperationalError: 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"?

How can I create/connect to a Postgres DB outside of the Postgres console?

Hi there, you'll have to adapt the instructions from the tutorial slightly, as the usernames, hostnames, and ports will all be different. You'll find the correct username, hostname, and port for your postgres server on the Databases tab...

Finally figured it out. In case anyone else has problems, I had to include the following:

    conn_string = "host='postgres_address' port=10494 dbname='DB_name' user='user_name' password='user_password'"

The postgres_address is the URL in your Postgres setting, while the DB_name, user_name, and user_password are whatever you created in the Postgrest console; the defaults are "postgres", "super", and the super's password.

Once you have that, you can call it with:

conn = psycopg2.connect(conn_string)

Thanks for posting that! One change that other people would need to make is to the port -- the number 10494 that you give is for your instance, so it differs between PythonAnywhere Postgres servers. The number that other people should use will appear on the "Postgres" section of their "Databases" tab.

Hello, I wanted to import my db locally on pythonanywhere

Hello, I wanted to import my db postgres locally on pythonanywhere

You can use the instructions here to basically back up your local postgres to a file, upload the file and then restore it on PythonAnywhere.