Externally connect to container's database

(Archon) #1

How can I connect to the database used by the container?
I’d like to connect from a PHP script for external authentication.

Pull user and password for custom app auth
(Florian Beer) #2

You’d need to expose the DB’s port to the host. But be sure to implement proper firewalling on the host, so only the desired hosts can access it.

(Archon) #3

Thanks, it’s working.

(Archon) #5

What is the default username/database name/password?

(Kane York) #6

The default is socket/identity/something like that-based authentication. Aka, there is no password.

Just spin up a rails console and do your manipulation/queries with ActiveRecord:

su discourse
cd /var/www/discourse
RAILS_ENV=production bundle exec rails c

> u = User.find_by_username_or_email('riking')
> t = Topic.find(13919)

(Archon) #7


I need a connection string like this one: host=sheep port=5432 dbname=mary user=lamb password=foo

(Archon) #8

I could set a password on postgresql server in docker, but I doubt it’s the actual solution to this.

(Kane York) #9

…actually… that is the real solution.

Or you could do the advanced setup for an external postgresql database, in which case the database isn’t in the container, and the whole thing is moot (you already have to have had superuser to set it up).

(Archon) #10

Okay, thanks, I’ll just set the password.

(C. R. Oldham) #11

I’m having trouble finding documentation for setting up Discourse with Postgres not located inside a docker container. Can someone point me in the right direction?

(Spero Koulouras) #12

The trick is in the app.yml. You need to set:

TODO: configure connectivity to the databases

DISCOURSE_DB_HOST: your db hostname
DISCOURSE_DB_USERNAME: 'your db user name’
DISCOURSE_DB_NAME: ‘your db name’

(Tomasz Lotocki) #13


You do not have to expose any ports to the outside world and play with firewall if you are on Digital Ocean.

When you set up your droplet on DO it asks you to add SSH key to the droplet, do this. it is called .ssh/id_rsa on your local machine.

Then before you start you need to set password for a database user as per @elberet answer: Is it possible to login into the postgres database inside the docker using navicat?

  • Enter the your droplet container.
  • su - postgres
  • psql -d postgres -c "ALTER USER postgres WITH PASSWORD '<new password>';"

After that you simply set up a SSH tunell in navicat / datagrip or whatever tool you use.

I hope it helps!

PS. backup your database before you start playing with it!