I'm trying to connect to my database using postgres on docker but it says that the password authentication failed.
My code:
const { Client } = require('pg');
const client = new Client({
host: 'localhost',
port: 5432,
user: 'root',
password: 'root',
database: 'mycontacts',
});
client.connect();
docker command to start the postgres container:
docker run --name postg -e POSTGRES_USER=root2 -e POSTGRES_PASSWORD=root2 -p 5432:5432 -d postgres
Try creating another Postgres container to try and connect and it will work.
Else. password authentication failed while trying to connect into my docker database
It ended up being the postgres that i had installed on my pc.
I uninstalled it and now its working!
Related
I'm trying to allow communication between my nodeJs docker image with my redis docker image (Mac OS X environment):
nodeJs Dockerfile:
FROM node:4.7.0-slim
EXPOSE 8100
COPY . /nodeExpressDB
CMD ["node", "nodeExpressDB/bin/www"]
redis Dockerfile:
FROM ubuntu:14.04.3
EXPOSE 6379
RUN apt-get update && apt-get install -y redis-server
nodeJs code which is trying to connect to redis is:
var redis = require('redis');
var client = redis.createClient();
docker build steps:
docker build -t redis-docker .
docker build -t node-docker .
docker run images steps flow:
docker run -p 6379:6379 redis-docker
docker run -p 8100:8100 node-docker
ERROR:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:907:11)
at exports._exceptionWithHostPort (util.js:930:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1078:14)
What should I do inorder to connect to Redis from node-docker?
Redis runs in a seperate container which has seperate virtual ethernet adapter and IP address to the container your node application is running in. You need to link the two containers or create a user defined network for them
docker network create redis
docker run -d --net "redis" --name redis redis
docker run -d -p 8100:8100 --net "redis" --name node redis-node
Then specify the host redis when connecting in node so the redis client attempts to connect to the redis container rather than the default of localhost
const redis = require('redis')
const client = redis.createClient(6379, 'redis')
client.on('connect', () => console.log('Connected to Redis') )
Docker Compose can help with the definition of multi container setups.
version: '2'
services:
node:
build: .
ports:
- "8100:8100"
networks:
- redis
redis:
image: redis
networks:
- redis
networks:
redis:
driver: bridge
Solution
const client = redis.createClient({
host: 'redis-server',
port: 6379
})
Then rebuild ur docker with => docker-compose up --build
When connecting in node, use redis-docker in the function argument you pass the server IP.
If you can create a new redis docker instance, try mapping the container port to host:
docker run --name some-redis -p 6379:6379 -d redis
docker container start some-redis
Now, you can start the container and connect with host 127.0.0.1
const redis = require("redis");
const client = redis.createClient({
host: '127.0.0.1',
port: '6379'
});
if redis installed then run command
sudo apt-get install redis-server
Then you will get your site running.
You can also change your /etc/hosts file to update the dockerip of the redis container.
Find the docker ip by using docker inspect
Download the redis server.
run the redis server.
and then run your project.
It should work just fine. Here's the download link:
Github - Redis Download Packages
I hope it works.
I have my back-end at Heroku. The connection file looks like this:
const knex = require("knex")({
client: "pg",
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: 5432,
ssl: {
rejectUnauthorized: false,
},
},
});
The listening:
server.listen(process.env.PORT || 5000);
At insomnia:
Insomnia route + body + error message
I'm able to test my application through the Heroku deploy without problems, but locally, i cannot. My env variables are configured, everything works fine, except the localhost requests.
Any ideias?
Just found out the problem guys. My dotenv wasn't working properly. Although my files were in the correct place, exactly how the documentation asks for, my .env wasn't being recognized (?). I found out this by console logging one of my environment variables. Found out to be undefined, so I pointed out the location of the .env file (default location, above src folder...), and everything started working as it should.
I'm trying to host a discord bot but it requires MYSQL2 as I need to do some database stuff. If I run this bot on my computer, it works fine. However, if I run it on my vps this error shows up:
Does anyone know why this is the case? It works very fine on my computer. I'm really struggling to find the answer. Please let me know if you know the solution to this :) Thanks, Joony.
I ran into a similar problem where the connection was being rejected. Found that changing the host from 'localhost' to '127.0.0.1' solved the problem.
For example:
const db = mysql.createConnection({
host: "localhost",
user: "username",
password: "password",
database: "myDatabase"
});
became:
const db = mysql.createConnection({ host: "127.0.0.1", user: "username", password: "password", database: "myDatabase" });
how to Connect postgresql using bookshelf.js from a remote system instead of using "host: localhost"
Use the hostname or IP address of the host, instead of 'localhost', like:
host: "dbserver.example.com"
or
host: "203.0.113.1"
Note: if the database is not using the default port (5432) you should also specify it. For example for port 5050:
host: "dbserver.example.com:5050"
or
host: "203.0.113.1:5050"
I have a database I generated through postgres. How do I connect it to sequelize for node.js?
I know the basic syntax for sequelize connections, but how do I connect these together?
In node.js after installing the sequelize and pg packages through npm you can do this:
var sequelize = new Sequelize('your_database_name', 'user', 'password', {
host: "localhost", //your server
port: 12345 //server port
dialect: 'postgres'
});
Note, in order to use Sequelize with Postgres you'll_need the postgres package. The Sequelize homepage contains instructions on changes you might need to make in order to use them together.