我启动了服务器。安装了nodejs,nginx,postgres,ufw等等。网站运行正常。连接到postgres时出现问题。我启动了服务器。安装了nodejs,nginx,postgres,ufw等等。网站运行正常。问题是连接到postgres。它在连接中,而不是在查询中,因为我尝试了许多sql查询,但它只是忽略了client.query()。Postgres默认设置和配置。我什么都没改变。
在配置文件中,#listening_address是注释。
由node-postgres使用。
const { Client } = require('pg');
const client = new Client({
user: 'admin', //tried postgres
host: '127.0.0.1', //tried *, site ip
database: 'passport', //tried other DB
password: 'secretpass', //installed through $passwd postgres, #ALTER USER admin with password ''
port: 5432,
});
client.connect();
app.post('/confirm', urlencodedParser, function (req, res){
let uid = uuidv4();
let query= {
text : 'INSERT INTO users(uid, name, surname, birthday, gender, email, password, region, language) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9);',
values : [uid, user.name, user.surname, user.birthday, user.gender, user.email, user.password, user.region, user.region]
}
client.query(query, (err, result) => { // I have tried many sql queries, but it just ignores client.query()
// console.log('some text'); doesn't display
if (err) {
console.log(err.stack)
} else {
res.render('addsite', {user: user});
}
});
});
/etc/postgresql/9.5/main/postgresql.conf
# i didn't change anything
data_directory = '/var/lib/postgresql/9.5/main'
hba_file = '/etc/postgresql/9.5/main/pg_hba.conf'
ident_file = '/etc/postgresql/9.5/main/pg_ident.conf'
external_pid_file = '/var/run/postgresql/9.5-main.pid'
port = 5432
max_connections = 100
unix_socket_directories = '/var/run/postgresql'
ssl = true
ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'
ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'
shared_buffers = 128MB
dynamic_shared_memory_type = posix
log_line_prefix = '%t [%p-%l] %q%u@%d '
log_timezone = 'Etc/UTC'
stats_temp_directory = '/var/run/postgresql/9.5-main.pg_stat_tmp'
datestyle = 'iso, mdy'
timezone = 'Etc/UTC'
lc_messages = 'en_US.UTF-8'
lc_monetary = 'en_US.UTF-8'
lc_numeric = 'en_US.UTF-8'
lc_time = 'en_US.UTF-8'
default_text_search_config = 'pg_catalog.english'
/etc/postgresql/9.5/main/pg_hba.conf
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
请帮帮我!
发布于 2020-05-08 13:18:52
嘿,大家都需要用pg的connect
方法来连接数据库。
添加配置后:
client.connect()
只有这样,你才能开始查询你的数据库。
欲了解更多信息,请访问以下链接:https://node-postgres.com/features/connecting
发布于 2020-05-08 13:49:22
因为你的pg_hba.conf看起来没有被碰过。看看这几行:
# Database administrative login by Unix domain socket
local all postgres peer
# "local" is for Unix domain socket connections only
local all all peer
这告诉您,只有在对等身份验证模式下才能访问数据库。为此,您应该使用postgres凭据访问数据库。
$sudo su postgres
之后,应该会提示您输入linux中的超级用户的密码。密码成功后,您应该会看到您的bash提示标签更改为:
postgres@<yourlinuxinstance>
现在,您可以使用对等身份验证模式访问postgres数据库。
$psql -U postgres
#should login in postgres with success
postgres=#
在psql命令行中,您现在应该创建访问数据库的用户和密码。
CREATE USER foo WITH ENCRYPTED PASSWORD '-+mysecurepasswordforthisuser+-';
CREATE DATABASE passport;
GRANT ALL PRIVILEGES ON passport TO foo;
\q #exit psql shell
$exit #exit from su session with postgres username
之后,您必须访问pg_hba.conf文件并更改访问方法,将peer
更改为md5
# "local" is for Unix domain socket connections only
local all all md5
重新启动数据库
$sudo service postgresql restart
之后,您可以在连接字符串中配置当前添加的用户密码和数据库。
现在,您应该成功地从nodejs代码访问数据库了。
如果你需要一些说明,你可以通过access this guide
https://stackoverflow.com/questions/61679981
复制相似问题