question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot connect to PosgreSQL using unix domain socket

See original GitHub issue

I can connect to PostgreSQL database using asyncpg with specifying DSN string as follows:

conn = await asyncpg.connect(
    'postgres:///sampledb?host=/cloudsql/xxx:asia-northeast1:yyy/.s.PGSQL.5432&user=postgres&password=pass')

(This PostgreSQL database is GCP Cloud SQL)

UsingTortoise.init(), however, I cannot connect to the database with the same DSN string.

await Tortoise.init(
    config={
        'connections': {
            'default': 'postgres:///sampledb?host=/cloudsql/xxx:asia-northeast1:yyy/.s.PGSQL.5432&user=postgres&password=pass'
        },
        'apps': {
            'models': {
                'models': ['__main__'],
                'default_connection': 'default',
            }
        }})

The code above raises OSError: Multiple exceptions: [Errno 61] Connect call failed ('::1', 5432, 0, 0), [Errno 61] Connect call failed ('127.0.0.1', 5432)

I also tried the following connection settings, but TypeError: __init__() missing 1 required positional argument: 'port' occurred.

await Tortoise.init(
    config={
        'connections': {
            'default': {
                "engine": "tortoise.backends.asyncpg",
                "credentials": {
                    "database": 'sampledb',
                    "host": '/cloudsql/xxx:asia-northeast1:yyy/.s.PGSQL.5432',
                    "password": "pass",
                    "user": "postgres",
                }
            }
        'apps': {
            'models': {
                'models': ['__main__'],
                'default_connection': 'default',
            }
        }})

How can I connect to the database using unix socket?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
grigicommented, May 10, 2020

Lets fix the issue of expecting a port at least?

1reaction
otsukacommented, May 10, 2020

I found that I can connect by giving port key to the credentials dict. Any number is ok for its value.

await Tortoise.init(
    config={
        'connections': {
            'default': {
                "engine": "tortoise.backends.asyncpg",
                "credentials": {
                    "database": 'sampledb',
                    "host": '/cloudsql/xxx:asia-northeast1:yyy/.s.PGSQL.5432',
                    "password": "pass",
                    "user": "postgres",
                    "port": -1
                }
            }
        'apps': {
            'models': {
                'models': ['__main__'],
                'default_connection': 'default',
            }
        }})

I have solved my problem.

If it’s not a bug that we can’t connect using a unix domain socket’s DSN, it’s ok to close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot connect to PostgreSQL unix domain socket
I've resolved the problem. The initial cause of it was in systemd service file for php-fpm /usr/lib/systemd/system/php-fpm.service:
Read more >
How to fix "psql: error: xxx and accepting connections on Unix ...
First, you need to make sure the socket file is located in /var/run/postgresql/.s.PGSQL.5432 . To check that $ cat /var/run/postgresql/.s.
Read more >
Can't connect to default unix socket (tries to connect to localhost)
The default behavior when host is not specified, or is empty, is to connect to a Unix-domain socket in /tmp (or whatever socket...
Read more >
BUG #16086: Cannot connect using psql, however I can ...
BUG #16086: Cannot connect using psql, however I can connect using pgadmin ... connections on Unix domain socket "/var/run/postgresql/.s.
Read more >
Apache cannot connect to PostgreSQL via Unix domain socket
Description of problem: When running under SELinux, Apache cannot connect to the unix domain socket PostgreSQL runs on.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found