I am trying to create a Rails app that uses PostgreSQL. Here is a description of what I did.
PostgreSQL setup:
I installed PostgreSQL 9.1.3 via the ppa:pitti/postgresql maintained by Martin Pitt. There was PostgreSQL 8.4 installed before; I am not sure if it is still installed or gone.
- I added a database user with superuser rights to the database that has the same name as my Ubuntu account.
- I start the database daemon with
sudo service postgresql start. - I installed pgadmin3, Version 1.14.0 Beta 1 via ppa:rhonda/pgadmin3 maintained by Gerfried Fuchs.
- I can connect via pgadmin3 using my user account and password and port 5433.
My postgres configuration in pg_hba.conf is as follows (removed comments for readability).
[...]
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Rails setup:
Now I want to create a Rails application that uses PostgreSQL.
- I installed Ruby 1.9.3-p125 via RVM.
- I installed Rails 3.2.3 into the Gemset ruby-1.9.3-p125@global.
- I created a .rvmrc and Gemset for the application.
- I created a Rails application via
rails new my_test_app -d postgresql. - I configured the
username andpasswordin config/database.yml for development and test and removed production. - I configured
host: localhostandport: 5433in config/database.yml.
Here is the content of my config/database.yml (removed comments for readability).
development:
adapter: postgresql
encoding: unicode
database: my_test_app_development
pool: 5
username: johndoe
password: password
host: localhost
port: 5433
test:
adapter: postgresql
encoding: unicode
database: my_test_app_test
pool: 5
username: johndoe
password: password
Problem:
However, when I run bundle exec rake db:create:all I receive the following error message.
could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket
"/var/run/postgresql/.s.PGSQL.5432"?
[...]
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode",
"database"=>"my_test_app_test", "pool"=>5, "username"=>"johndoe",
"password"=>"password"}
Question:
Why is the port different to the one I use when I successfully connect via pgadmin3?
This is probably not the main reason why the create_all() method call doesn’t work for people, but for me, the cobbled together instructions from various tutorials have it such that I was creating my db in a request context, meaning I have something like:
# lib/db.py
from flask import g, current_app
from flask_sqlalchemy import SQLAlchemy
def get_db():
if 'db' not in g:
g.db = SQLAlchemy(current_app)
return g.db
I also have a separate cli command that also does the create_all:
# tasks/db.py
from lib.db import get_db
@current_app.cli.command('init-db')
def init_db():
db = get_db()
db.create_all()
I also am using a application factory.
When the cli command is run, a new app context is used, which means a new db is used. Furthermore, in this world, an import model in the init_db method does not do anything, because it may be that your model file was already loaded(and associated with a separate db).
The fix that I came around to was to make sure that the db was a single global reference:
# lib/db.py
from flask import g, current_app
from flask_sqlalchemy import SQLAlchemy
db = None
def get_db():
global db
if not db:
db = SQLAlchemy(current_app)
return db
I have not dug deep enough into flask, sqlalchemy, or flask-sqlalchemy to understand if this means that requests to the db from multiple threads are safe, but if you’re reading this you’re likely stuck in the baby stages of understanding these concepts too.
Introduction
This article’s focus is mainly on how to solve an error message. The error message appear when executing the command for creating an new database. So, the following is the appearance of the output of creating a database :
-
First of all, the most important part is accessing the PostgreSQL database server. In this context, accessing the PostgreSQL database is possible by using a command in the command line. The aim is to login to the PostgreSQL command console :
[root@hostname ~]# psql -Uadmin Password for user admin: psql (11.10) Type "help" for help. postgres=>
-
Create a new database in the PostgreSQL command console by executing the following query :
postgres=> create database mydb; ERROR: permission denied to create database postgres=> q
Sadly, but it is true, the command for creating a new database does not work as usual. So, where does it go wrong ?. So, there must be a solution to solve the problem above. As the error message is indicating a permission issue, there might be a connection with the role of the user executing the command.
Solution
The solution is quite simple. Since there is a slight connection with the permission issue, below are steps to solve the problem :
-
First of all, just check the permission of the user from the PostgreSQL command console by accessing the PostgreSQL command console as follows :
[root@hostname ~]# psql -Uadmin postgres Password for user admin: psql (11.10) Type "help" for help. postgres=#
-
After that, type the follwoing command to change the permission of the user. Just execute the command to add a role for creating database. The role name is ‘createdb’. The query pattern for altering the user’s permission exist as follows :
postgres=# du List of roles Role name | Attributes | Member of -------------+------------------------------------------------------------+----------- admin | | {} centos | Cannot login | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} dbadmin | Superuser | {} postgres=# -
So, as in the above output, it is very clear that the user ‘admin’ does not have any role attributes at all. So, add a new role attribute to the user. Login first as postgres or as a superuser account by typing the following command :
[root@hostname ~]#psql -Upostgres Password for user postgres: psql (11.10) Type "help" for help. postgres=#
-
Soon after, execute the command to add the createdb role as follows :
postgres=# alter user admin createdb; ALTER ROLE postgres=# q
-
Next, connect to the PostgreSQL command console once more using the first user. In this context, it is the user with the name of ‘admin’ as follows :
[root@hostname ~]# psql -Uadmin postgres psql (11.10) Type "help" for help. postgres=>
-
Finally, create the database by executing the following query :
postgres=> create database mydb; CREATE DATABASE postgres=> q
-
Last but not least, list the database to check whether the new created database exist or not by typing the following query :
postgres=> l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -------------+----------+----------+----------------------------+----------------------------+-------------------------- mydb | admin | UTF8 | English_United States.1252 | English_United States.1252 | postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 | simpeg | postgres | UTF8 | English_United States.1252 | English_United States.1252 | template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres + | | | | | postgres=CTc/postgres (9 rows) postgres=>
At last, the new created database exist with the name of ‘mydb’.
createdb needs to connect to a database to issue a CREATE DATABASE SQL statement, and by default it will use:
- a Unix socket domain connection
- the current username from the shell as the database user (so
postgresin your case) template1as the database
When it does that, in your case you get this error message:
createdb: could not connect to database template1: FATAL: no
pg_hba.conf entry for host «[local]», user «postgres», database
«template1″, SSL off»
It indicates that postgres as a database user does not have the permission to connect through a Unix domain socket (that’s what [local] means).
This is unusual, since in general, postgres installers on Unix tend to install a pg_hba.conf starting with this rule:
local all postgres peer
Meaning that the postgres Unix user has the right to connect locally to any database as the postgres database user, which suits the needs of administrating databases, including creating new ones.
In fact your pg_hba.conf does not look like a default one. The simplest way to proceed to immediately solve the problem of database creation would be to edit it, add the above line as the first rule, and reload the postgres service.
I’m hoping I’m just doing a dumb-dumb, but I can’t seem to create the db with sequelize-cli.
C:repospostman>npm run db:create
> postman@0.0.0 db:create C:repospostman
> sequelize db:create && npm run db:migrate && npm run db:seed
Sequelize CLI [Node: 8.9.4, CLI: 3.2.0, ORM: 4.28.0]
WARNING: This version of Sequelize CLI is not fully compatible with Sequelize v4. https://github.com/sequelize/cli#sequelize-support
Loaded configuration file "serverconfigconfig.json".
ERROR: Dialect undefined does not support db:create / db:drop commands
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! postman@0.0.0 db:create: `sequelize db:create && npm run db:migrate && npm run db:seed`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the postman@0.0.0 db:create script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:UsersdhinesAppDataRoamingnpm-cache_logs2018-04-26T16_11_26_029Z-debug.log
Thoughts?
Make sure the dialect property in config.json is
«postgres»
and that it didn’t get changed to something else (e.g. postgresql).
Here’s an example config file:
{
"development": {
"username": "postgres",
"password": "secret1",
"database": "postman_dev",
"host": "localhost",
"port": 5432,
"dialect": "postgres"
},
"test": {
"username": "postgres",
"password": "secret1",
"database": "postman_test",
"host": "localhost",
"port": 5432,
"dialect": "postgres"
}
}
That’s exactly what I have in my config file. It almost seems like it would be an issue with the sequelizer-cli version or something, but I’m using your lock-file. What version of Postrgres are you using?
I believe I was using version 10.
I wonder if there’s a problem loading the config file, because it’s saying the dialect is «undefined»
Yeah, it’s got to be something like that. It’s a bit of a long shot but what about Node version? I’m on Node v8.9.4 and and npm v5.6.0.
I think I was on node 9 when I created the course, I know I’ve since upgraded to 9.8 and haven’t had problems.
I’d tell you to just create the DB that config file is used for all the queries so you wouldn’t be able to do anything with the DB.
Which command did you run that produced this error?
Do you have a repo I can clone and try to debug?
I won’t have access this weekend; however, the the only changes I made to
your repo we’re updating the password in the config. It’s exactly the same
otherwise. If I understand how lock files work, I should have had the exact
versions of every package you used, right? That means it has to be my
environment (Node, Windows, something…), correct?
…
Yeah, it must be something on your environment. I just pulled latest, and spun up a psql DB, set my password to «secret» and tried it, and the database was created without issue.
I know that people have done the course on Windows, so I’m kind of at a loss for why it’s not working for youl
I’m getting what I need from the course without following along in your
repo (all the pieces are mapping 1:1 to my current project, so it’s working
out just fine). It hurts my pride to let a bug win, but I’m happy to chalk
this one up to a loss (heck, it could be some crazy settings on the
corporate network, since this is at work) and just close this issue until
someone else runs into the same.
…
On Fri, Apr 27, 2018, 9:06 PM Nathan Taylor ***@***.***> wrote:
Yeah, it must be something on your environment. I just pulled latest, and
spun up a psql DB, set my password to «secret» and tried it, and the
database was created without issue.
I know that people have done the course on Windows, so I’m kind of at a
loss for why it’s not working for youl
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AYEAFw0pyqRLCjPifmntk1vHBuh57j7Rks5ts8CPgaJpZM4Tl1f4>
.
Hi Taylor, I am new to pluralsight and the course, I am running into a issue with the config/config.json when I run the npm run db:create command. The error is «Error: Cannot find module ‘/my_path/postman_projectName/config/config.json'» I am running Node 9.10.1 and npm 6.4.0 using Visual Code. Also I have uninstalled the node modules and reinstalled as well. Any suggestions or thoughts on this issue?
Thanks in advance
What is the path that you’re running npm run db:create from?
Hi Nathan,
I cd into the project from home its «home/myname/angularprojectsdir/postman» or $ cd home/myname/angularprojectsdir/postman.
I’ve also tried $ cd angularprojecstdir/postman/server so that I am directly in the server folder, but that didn’t work either.
In addition to that I found the reference to the path in the downloaded repo in the index.js file and tried different path calls by changing that to server/config/config.json or just config/config.json and even /../../config/config.json.
I am running a Ubuntu 16.04 environment if that helps.
I’m not sure.
From your bash shell, you can cd into my_path/postman_projectName/config/ and is there a config.json file in there?
I was running into this issue and removed the username from the config file altogether and it worked.
{
"development": {
"username": "",
"password": null,
"database": "postman_dev",
"host": "localhost",
"port": 5432,
"dialect": "postgres"
},
"test": {
"username": "",
"password": null,
"database": "postman_test",
"host": "localhost",
"port": 5432,
"dialect": "postgres"
}
}
try it
$ npm run db:create --env=test
try it
$ npm run db:create --env=test
Thanks, the issue was fixed. But every time I have to add —env=development, how can I make that sequelize-cli get this environment variable from a persistent source?
I’m going to refer to the comment before yours almost 2 years ago. This project no longer uses postgres, and therefore no longer uses a postgres database.
Today I tried to setup Postgres as my db for my Rails project, and met with this error:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"my_cute_db", "pool"=>5, "username"=>"rongjun", "password"=>nil}
PG::Error: ERROR: permission denied to create database
I want to write down how I solved it.
What I intended to do is the create a db called my_cute_db with rake command, rake db:setup. However, it’s giving error like shown above.
Here’s my database.yml
development: adapter: postgresql encoding: unicode database: my_cute_db pool: 5 username: rongjun password:
My username is rongjun. And the error is telling me that “permission denied to create database“, so I have to go into psql command-line interface to find out the permission of my role.
my_terminal# psql postgres
postgres=# du
List of roles
Role name | Attributes | Member of
------------+------------------------------------------------+-----------
rongjun | | {}
daodaowang | Create DB | {}
kai | Password valid until infinity | {}
tanjunrong | Superuser, Create role, Create DB, Replication | {}
validator | | {}
du command is listing down the role names and their privileges. We can see that rongjun is not having any privileges. On the other hand daodaowang is able to Create DB. Now we would like to enable rongjun to be able to Create DB just like daodaowang.
postgres=# ALTER ROLE rongjun WITH CREATEDB;
Then we run du again to check:
postgres=# du
List of roles
Role name | Attributes | Member of
------------+------------------------------------------------+-----------
rongjun | Create DB | {}
daodaowang | Create DB | {}
kai | Password valid until infinity | {}
tanjunrong | Superuser, Create role, Create DB, Replication | {}
validator | | {}
rongjun is now having the correct privilege to Create DB. Let’s run rake db:create again, it will do the job without error now. You can check by running psql -l command to list down all databases you have.
After creating the db, you can grant more privileges to your user:
postgres=# GRANT ALL PRIVILEGES ON DATABASE my_cute_db to rongjun;
Reference:
For how to create database refer to: http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/
For creating the user with ‘Create DB’ privilege directly without having to ALTER it later:
create user any_username with password 'any_password' createdb;