Arc Forumnew | comments | leaders | submitlogin
Issue connecting to local postgres db from Arc app
3 points by markkat 998 days ago | 9 comments
Hey all. I am having an issue calling my postgres database on the same machine as my arc app.

I can connect to the db from the arc command line, but when it connects via the app, (pipe-to (system "psql -U user -d database") (prn "...") it times out, throwing:

srv thread took too long for x.x.x.x user break context...: /usr/share/racket/collects/racket/system.rkt:174:0: do-system*/exit-code /usr/share/racket/collects/racket/system.rkt:213:0: system /home/xapp/ac.scm:1088:20 g1987 g1986 gf1497 handle-request-thread

I can connect by command line via "psql -h url -U user" or by (system "psql -U user -d database") (prn "...") in the arc command line, but not from the app itself.

Any insight would be appreciated!

BTW I have ufw disabled.



3 points by krapp 997 days ago | link

There is a database wrapper (database.arc) in /lib that should support postgres.

It isn't well documented and only tested on a SQL connection but it's just a set of wrappers around Racket's DB drivers, so you could try including that into your app.

Unfortunately I seem to have completely lost my test project but the connection should work as follows:

    (= dbconn (postgresql-connect user db password))
Hope that helps.

-----

1 point by markkat 996 days ago | link

Thanks! So far no avail, but I'll try some more!

btw I caught a misspelled 'password' in postgresql-secure-connect:

  (mac postgresql-secure-connect (user db ssl-protocol (o passowrd nil))

-----

1 point by krapp 995 days ago | link

>btw I caught a misspelled 'password' in postgresql-secure-connect:

oops. It's fixed, thanks.

-----

1 point by markkat 994 days ago | link

btw, would this be the correct syntax for table-exists?

  (= dbconn (postgresql-connect "root" "root" password))
  (table-exists? dbconn "items")

-----

1 point by krapp 994 days ago | link

yes.

-----

3 points by markkat 994 days ago | link

UPDATE

I got it to work! I believe akkartik was right. Using command (-c) did the trick, which I assume ensured disconnecting from the database.

  (system "psql -U root -d root -c 'SELECT * FROM items'")
returned

  item_id | name | type | description | created | location ---------+--------+------+-------------------------------------+----------------------------+---------------------------------------------------- 1 | marble | tool | A small, translucent orange bauble. | 2022-02-23 17:59:45.480545 | 0101000020E610000012C2A38D239A5EC040683D7C99E44240 (1 row)
Thanks!!!

-----

2 points by akkartik 997 days ago | link

Hi Mark!

Is this on hubski.com? Have y'all been using postgres there for a long time? Or are you trying it for the first time?

Can you show what command you're running to check that you "can connect to the db from the Arc command line"? I wonder if the app is able to connect but not disconnecting for some reason. That would explain the "thread took too long" timeout message.

If you've written some custom code to connect to postgres, that'd be helpful to look at as well.

-----

2 points by markkat 996 days ago | link

Hey Kartik!

It's a new project. However, Hubski has been using postgres for a few years, but it's currently an amalgam of Arc and Racket. I didn't write the db code, and am trying to start fresh. The Hubski racket connection looks like:

  (define db-conn
  (virtual-connection
   (connection-pool
    (lambda () (postgresql-connect
           #:user db-user
           #:password db-pass
           #:database db-database
           #:server db-server
           )))))
with

  (define db-user     "user")
  (define db-pass     "password")
  (define db-database "hubski")
  (define db-server   "localhost")
>Can you show what command you're running to check that you "can connect to the db from the Arc command line"?

Here's my test code that works in the command line, and the response. (I named my db "root" atm and will change that and the user once I get it working. I'm running the app as root.):

  arc> (tostring (pipe-to (system "psql -U root -d root") (prn "SELECT * FROM items WHERE name = 'marble';"))                                     )
  " item_id |  name  | type |             description             |          created           |                                                           
  location                      \n---------+--------+------+-------------------------------------+--                                     ------------ 
  --------------+----------------------------------------------------\n       1 | marble | tool | A small, translucent orange bauble. | 2022-02-23 
  17:59:45.480545 | 0101000020E610000012C2A38D239A5EC040683D7C99E44240\n(1 row)\n\n"
  arc>
But when I have that same code in the app, it times out. I tried to add (prn "\\q") at the end, but same result. Works in command line but hangs up in the app.

I even tried to specify the host and port:

  (pipe-to (system "psql -U root -h 159.203.186.97 -p 5432 -d root") (prn "SELECT * FROM items WHERE name = 'marble';"))
With this in my pg_hba.conf

  host    all             root            159.203.186.97:5432      trust
And the same result; returns in command, app times out. Checked the port:

  root@Xyrth:/home/xapp# netstat -tulnp | grep 5432
  tcp        0      0 159.203.186.97:5432     0.0.0.0:*               LISTEN      124232/postgres
Thanks for the quick reply!

BTW, I am using the domain xyrth.com with a nginx reverse proxy. I just had the thought it may be my nginx configuration. It probably needs to listen explicity on 5432? Here's my current sites-enabled:

  server {
    listen 80;
    server_name xyrth.com;
    return 301 https://xyrth.com$request_uri;
  }

  server {
    	listen              443 ssl;
    	server_name         xyrth.com;
        ssl_certificate "/etc/letsencrypt/live/xyrth.com/fullchain.pem";
        ssl_certificate_key "/etc/letsencrypt/live/xyrth.com/privkey.pem";
        ssl_session_cache shared:SSL:5m;
        ssl_session_timeout  10m;

        proxy_redirect      off;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    Host $http_host;

        location / {
                proxy_buffers 16 4k;
                proxy_buffer_size 2k;
                proxy_pass http://159.203.186.97:8080;
		proxy_set_header Host xyrth.com;
        }

        location ~* \.(png|jpg|tif|ico|ttf|woff|svg|eot|otf|mp3|ogg|wav)$ {
        root /home/xapp/static;
        expires max;
        }

        location ~ ^/(xyrth.css) {
                root /home/xapp/static;
                expires max;
        }

  }

-----

1 point by markkat 995 days ago | link

Killing me. I don't think it's nginx..

Maybe it isn't disconnecting?

  (tostring (system "psql -V"))
Returned

  psql (PostgreSQL) 12.9 (Ubuntu 12.9-0ubuntu0.20.04.1)
just fine from the app.

-----