I am brand-new to Sails and I'm looking to build my first application in javascript land! I am currently trying to connect my sails application to a postgresql database and I want to make sure that I am doing this correctly.
I started by creating a postgresql db with dbName, userName, password. I have added all this information into my connections.js file:
somePostgresqlServer: {
adapter: 'sails-postgresql',
host: 'localhost',
user: '<username>', // optional
password: '<password>', // optional
database: '<databasename>' //optional
}
I want to be sure I can migrate and perform all operations on my own so my models.js is set to migrate: safe. I ran npm install sails-postgresql. Now, to my understanding if I have migrate set to safe I will need the sails-db-migrate module (https://github.com/building5/sails-db-migrate). I followed this module step by step. I generated a User model by running sails generate api user. After this I ran grunt db:migrate. After all this, I check my psql database and no User table has been created. I know there is something I am missing, or maybe there is a more simple way to see if my postgresql db is connected.
Any advice or suggestions on how I should approach this would be greatly appreciated.
I was able to connect my PSQL DB to my application by simply:
creating db in psql
configuring it in sails app/connections.js
running npm install sails-postgresql
Adding to config/env/development.js:
models: {
connection: 'somePostgresqlServer'
}
running sails generate api user
sails lift
When asked for migration mode I chose options 2 which is alter
Opened up psequal and sure enough there was my new user table.
I'm not sure which of these actually triggered it to working from adding my db name to development.js, leaving off a PW on my DB, or changing migration mode to alter. But these steps helped it to connect. Will look further into.
Related
I am new to sails and its connection to database. I plan to use Heroku to host my app so I would like to use Postgresql.
I have changed config.datastore file with the following code:
default: {
adapter: 'sails-postgresql',
url: 'postgresql://postgres:postgres#localhost:5432/postgres',
max: 1
}
On Sails documentation it sais that url is url: 'postgresql://user:password#host:port/database', but I dont understand where should I get user and password from. Do I need to initiate the postgresql and set it up and define a new database?
I am just trying to understand the mechanics behind this setup.
What you need to do is create a new user for your local PostgreSQL instead of using the default user and database. What I mean is, create a user-specific to your particular project in SQL following this should help https://www.postgresql.org/docs/8.0/sql-createuser.html.(This should also help you set the password)
Then create the database you want to connect to your Sails app to be used by Waterline by following this: https://www.postgresql.org/docs/9.0/sql-createdatabase.html (make sure you grant the user you created permissions for this database as well https://www.postgresql.org/docs/9.0/sql-grant.html)
Then all you need is replace the url property value in config/datastore.js with the value following the format
postgres://USERNAME:PASSWORD#localhost:5432/DATABASE_NAME
N.B: Replace USERNAME, PASSWORD, and DATABASE_NAME with the actual username, password, and database name respectively.
When you are deploying to Heroku, Heroku would provide you with an environment variable called DATABASE_URL if you install the Heroku Postgres add-on. You could just pass that to url like so
url: process.env.DATABASE_URL
My problem is that I'm new in JHipster. I can construct a basic application using the H2 disk persistance method to develop and it works fine, but when I try to build a new project using the Postgres db as development method, and I configure de database attributes in the application-dev.yml file, it just doesn't work, I can't even log in.
Can anyone help me with this?
Here is the fragment of the application-dev.yml file where I configure the access db. Of course I've already created the database (ControlAcceso), but it's empty, I thought the tables creation will be automatic, please correct me if I'm wrong.
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:postgresql://localhost:5432/ControlAcceso
username: postgres
password: root
hikari:
poolName: Hikari
auto-commit: false
If you are able to use docker you could add testcontainers when using psql also in dvelopment to get similar experience like using h2 https://atomfrede.gitlab.io/2019/05/jhipster-with-testcontainers/
I am new to ansible (2.2.1) and have started to migrate from our fabric scripts to ansible which I find somewhat better regarding structure. I have run into an issue, it should be pretty straight forward but since I do not know ansible through and through I am not sure how to proceed. I am running this against a vagrant box as of now.
The issue is regarding user privileges and postgres.
Lets say I have this playbook
- hosts: web
become: yes
become_user: root
vars:
dbname: myapp
tasks:
- name: ensure database is created
postgresql_db: name={{dbname}}
I cannot make this simple example work! All dependencies are met. If I do the same thing with mysql this works fine but here I get issues with unable to connect to database: FATAL: Peer authentication failed for user "postgres".
In mysql I use the "root" user with a blank password, which works because I know that user is created upon install with a blank password.
There is a user postgres created when the installation of postgresql is completed so the user exists. And as root I should be able to login by saying I am the postgres user. Am I missing something in how this is done? It works just fine if log into the server and sudo -su postgres && psql.
I also tried to add become_user: postgres by the task I want to run but then I get unprivileged user issues.
Any ideas of what is missing?
Found a few workarounds and a solution, given you are ok with giving away some security (which makes sense, this being an issue just for that reason).
More people having this problem with the new ansible in this github issue thread
https://github.com/ansible/ansible/issues/16048
What I ended up doing was settings allow_world_readable_tmpfiles = True in the ansible.cfg file. Then this is not an issue anymore but you get warnings. I only need this setting for when handling the postgres role, so maybe I'll end up split it up or putting the setting somewhere less global.
I am having trouble with my migration to Ubuntu Linux. I can use Postgres in the terminal. So I don't have a problem with the Postgres password.
When I type: knex migrate:latest --env development
I get:
Using environment: development
Knex:warning - Pool2 - Error: Pool was destroyed
Knex:Error Pool2 - error: password authentication failed for user "user"
I've read from other answers in related questions to go into the pg_hba.conf and set the method to trust. I've done this, but no change.
my knex.js file looks like this:
module.exports = {
devolopment: {client: 'pg',
connection: 'postres://localhost/bikesdb'
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL
}
};
I'm not sure what I'm doing wrong.
You are most likely running on node 6.x.x so you'd need to upgrade your pg package version npm install --save pg#4.5.5
Reference: https://github.com/tgriesser/knex/issues/1371
If Andrei Stalbe's solution doesn't help... here are some more thoughts.
How are you connecting to postgres from commandline?
If you are writing just psql bikesdbit actually uses unix socket for connecting and it has different security settings in postgres by default than through TCP. You can try to change your connection string to: postgres:///bikesdb which makes also knex to use unix socket.
You can change/check security policies in pg_hba.conf.
Also you have typo in postres://localhost/bikesdb.
EDIT:
I'm pretty sure you are still having some general postgresql configuration problem. If you like to create user to access your database you can do this:
CREATE ROLE bikesuser WITH LOGIN PASSWORD 'letmepass';
GRANT ALL PRIVILEGES ON DATABASE "bikesdb" TO bikesuser;
And change connection string to:
'postgres://bikesuser:letmepass#localhost/bikesdb'
If you like to allow access from localhost to postgresdb this should do it in pg_hba.conf
host all all 127.0.0.1/32 trust
Hope that something of this is useful, even that it sounds that you have already tried this all.
I had the same problem and it was all about the fact that knex was trying to use the username taken from the environment variables (LOGNAME) of my command line interface.
If Postgres is set to use a different user (by default postgres if you haven't changed it) there will be an authentication error.
You can solve this problem by specifying the authentication credentials inside the URL that you use to connect to the Postgres server.
module.exports = {
devolopment: {client: 'pg',
connection: 'postres://user:password#localhost/bikesdb'
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL
}
};
I highlight the key point in the URL:
postres://user:password#localhost/bikesdb
You can check the name of the user to access the Postgresql server from the UI interface provided by Postgresql and the password is the one that you have defined during the installation phase (if you haven't changed it)
I ran into this problem recently. My issue was I could run migrations on local macOS but not on a remote Linux Ubuntu machine. I found out that the reason was I didn’t have a password set for the user, though everything works fine on local for some reason it required a password on remote (Linux). So, setting password for the user and adding the password to the connection solved my problem.
Example:
On postgres
\password username
Then enter and confirm the password.
In your config file (e.g knexfile.js)
module.export = { client: ‘postgresql`, user: username, password: secret }
I had set my psql to go to my user instead of postgres. However, I reset the postgres password. I just reset the postgres password, and the in the development connection I included the password. (I tried including it before the postgres password reset and it still didn't work). It's odd because the user I specified in the connection was me and not postgres.
I am trying to use sails.js on Heroku. When I push my changes, the application starts, but when I try to run MyModel.find() methods, I receive an E_NOTFOUND error. When I log into the database with psql, I see that the tables have not been created automatically. I have the policy in models.js set to migrate: 'drop', so shouldn't I at least get empty tables made when I launch the application? Is there something going on on Heroku that it doesn't like me running sails?
edit:
I had previously been putting settings in development.js and production.js that were different settings (heroku postgresql settings in production). I took that out, and put the settings in connections.js, and it seems like I am able to do the queries and such on the Models, however, when I do heroku pg:psql to connect to my database, I don't see the tables if I do "select * mytable;" it tells me no relation is found.
I believe the problem was that even though I had separate settings for development or production databases, when NODE_ENV was set to production, sails defaults to "safe" and will not let you change it to alter or drop - silently. Changing NODE_ENV to something else allowed the tables to be created.