Gitlab will not create Postgres instance on alternative port - ruby-on-rails

I am running the latest version of the GitLab CE Omnibus package on CentOS 7. On that same machine I have other Postgres database running. I seem to be unable to change the port of the in-build Postgres instance to 5433, for example.
I have added the two following lines to the /еtc/gitlab/gitlab.rb
postgresql['enable'] = true
postgresql['port'] = 5433
And I have executed the following commands:
# gitlab-ctl reconfigure
# gitlab-rake gitlab:setup
The second of which fails with the following error:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"gitlabhq_production", "pool"=>10, "username"=>"gitlab", "password"=>nil, "host"=>nil, "port"=>5433, "socket"=>nil}
-- enable_extension("plpgsql")
rake aborted!
PG::Error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5433"?

I don't use gitlab, so I looked over the installation docs. GitLab doesn't have "its own" PostgreSQL installation. It uses a standard install (a package-based install) for PostgreSQL.
sudo apt-get install -y postgresql postgresql-client libpq-dev
So you could approach your problem in two different ways. Since you already have PostgreSQL installed and running on port 5432 (?), you could just configure GitLab to use that instance.
Or you could set up your new installation of PostgreSQL to listen on port 5433 (set in postgresql.conf), and configure GitLab to use that instance.
Regardless of which way you go, you have to get the listening configuration (PostgreSQL) and the talking configuration (GitLab) to match, and you have to get them both to agree on authentication. PostgreSQL controls authentication through the configuration file pg_hba.conf. Ruby on Rails controls authentication through the configuration file database.yml. I presume GitLab uses some combination of database.yml and gitlab.rb, but I'm not sure about that.
Both PostgreSQL and Rails write log viles that you'll find useful in tracking down the root cause of error messages. PostgreSQL settings are in postgresql.conf (search for "ERROR REPORTING"). The default log file is /var/log/postgresql/postgresql-9.4-main.log for version 9.4 under Ubuntu Linux; CentOS might use a different directory. Rails writes to the project's log/ directory (same level as app, config, test, etc.)

Related

rails database setup command can not make the database as I wish?

I am newbie with Ruby Rails and here is my problem.
I created a very simple Rails program by this command
rails new freelancer --database=postgresql --javascript=webpack
And I had no error
In this file
config -> locales -> database.yml
I saw 2 files
database: freelancer_test
database: freelancer_development
Then I ran this command
rails db:setup
It showed me this error
Is the server running locally and accepting connections on that socket? Couldn't create 'freelancer_development' database. Please check your configuration. rails aborted! ActiveRecord::ConnectionNotEstablished: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket? Caused by: PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket? Tasks: TOP => db:setup => db:create (See full trace by running task with --trace)
I thought that it must created 2 databases freelancer_test and freelancer_development ?
Could you please give me some advices ? Thank you in advance.
A Postgresql server is required and configured properly to run this Rails application. From the error it expects a postgres server running locally. But you can also connect to a standalone DB server remotely.
Your database.yml should contain correct connection info then run the same command again.
It means that you have no PostgreSQL instance running locally.
Two choices:
Either you install it, you can follow this nice tutorial: https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-macos
Or you can just use SQLite for you dev environment
Unless you're really need PostgreSQL features for your development, I'd go for the second option, as you're beginner with RoR, you probably want to focus on the development of your app first. Using the default sqlite database requires no dependencies as the database is a text file in the end.
You can create the app with the following command:
rails new freelancer --javascript=webpack

PostgreSQL error: could not connect to database template1: could not connect to server:

I'm running into the following error after uninstalling and installing several times I just can't seem to get postgresql working. I am trying to setup the coding environment for ruby on rails 6 on windows 10. I have followed the following articles and have made it all the way to creating a user where i get the following errors.
links to how i got here
https://github.com/serivas/wsl2_rails_setup
https://gorails.com/setup/windows/10#ruby-rbenv
https://www.postgresql.org/download/linux/ubuntu/
Error I'm getting when i type
sudo -u postgres createuser abc -s
Error Message;
createuser: error: could not connect to database template1: 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"?
Thanks in advance!
Here's how I solved my problem for anyone that might be having similar issues.
Make sure that your windows 10 is updated to be able to run WSL2
First follow the link below to get Ruby and Rails installed.
https://gorails.com/setup/windows/10
Setup PostgreSQL from the link above,
the link below also help me setup PostgreSQL;
https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database
If your having trouble logging into Postgres make sure that you create a user in Postgres that allows the user to be able to CREATEDB. So if your user is JohnDoe make sure that you give permission for JohnDoe to create database.
Links to help you out with granting permission.
https://dba.stackexchange.com/questions/33285/granting-a-user-account-permission-to-create-databases-in-postgresql
Make sure that you are starting your postgresql server.
sudo service postgresql start
And to Stop
sudo service postgresql stop
After User has rights to create the database, then you can go ahead and type in
rake db:create

Rails 6 is unable to connect to AWS Elastic Beanstalk provisioned RDS. Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"

I am having a very difficult time trying to launch a sample Rails 6 application to Elastic Beanstalk. For context, I am following these instructions
ADD RDS to Ruby Application
ADD an RDS to Beanstalk
I have followed these instructions to a tee and am still unable to connect to the rds database that I have provisioned. I keep receiving the following error:
PG::ConnectionBad: 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"?
Whenever I try to run RAILS_ENV=production rails db:migrate or any other rake task, I keep getting that error.
On my AWS console, under Configuration and Software, I have the following environment variables:
Also in my database.yml file I have the rds configured variables listed as such.
production:
adapter: postgresql
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
I have mapped my values as instructed in the documentation and am certain that they are correct.
Finally, I have sshed into my beanstalk provisioned ec2 instance and have executed the following command:
psql -U username -p 5432 -h examplehost.rds.amazonaws.com -d ebdb
provided the password and am able to connect. I am really at my wits end, I've spent too much time trying to diagnose this and am running out of ideas. I don't know where to look too next for ideas on how to trouble shoot this. I've read so many stack overflow questions and blogs that my head is spinning. If anyone has any ideas on how to resolve this, I would greatly appreciate it.
---Update----
I have created a new environment variable on the elastic beanstalk console.
ENV['DATABASE_URL'] = postgres://YourUserName:YourPassword#YourHostname:5432/YourDatabaseName
I made the necessary configurations, uploaded my .zip file and the connection to the database failed.
---- UPDATE-----
printenv does not show the varialbes provided by beanstalk, however this command does sudo /opt/elasticbeanstalk/bin/get-config environment.
My first advice is that, in my opinion, it is a much better option to create an Amazon RDS on their own, and not tied to Beanstalk.
As the AWS documentation indicates (emphasis mine):
AWS Elastic Beanstalk provides support for running Amazon Relational Database Service (Amazon RDS) instances in your Elastic Beanstalk environment. To learn about that, see Adding a database to your Elastic Beanstalk environment. This works great for development and testing environments. However, it isn't ideal for a production environment because it ties the lifecycle of the database instance to the lifecycle of your application's environment.
And:
To decouple your database instance from your environment, you can run a database instance in Amazon RDS and configure your application to connect to it on launch. This enables you to connect multiple environments to a database, terminate an environment without affecting the database, and perform seamless updates with blue-green deployments.
In my opinion, even for testing or development, it is always advisable to configure a small database instance and give your application the ability of define the most appropriate mechanism for connecting to your database.
The only downside is that you will probably need to configure a VPC, although it should not be actually a problem and, in ay case, it is worth value.
If for any reason you need to use the Beanstalk provisioned RDS database perhaps you have some workarounds to your problem (it should be a workaround because your configuration looks fine - please, only, verify that the database configuration is defined for the right Beanstalk environment).
For instance, one thing you can try is to store the database connection configuration in a S3 bucket, as also suggested in the AWS documentation. The idea is basically create some configuration file with the necessary connectivity information, store it in S3, and read that configuration in your application, i.e., process that file, in order to initialize your database.
But maybe you can try another approach.
Please, consider this SO question, and the answer from Jon McAuliffe and others. As indicated, Beanstalk will provide your application with environment variables, but maybe this variables will not be exposed as shell variables, they will be exposed to your application in different ways depending on the runtime the application needs to be executed on.
In the case of Ruby, you are accessing these variables in the correct way but, for any reason, your program is not having access to that information.
This probably also explains why printenv does not print any if your variables but the get-config script does.
But maybe you can take advantage of the fact that get-config provides you the right information and, either define this variables in your ENV by executing the get-config script for every RDS* key, perhaps in your environment.rb - please, be aware that I programmed in Ruby when I was a student but there is a long time since that, do the task in the file you consider appropriate - or using .ebextensions and a custom configuration file. You can find several examples here.
For instance, consider the following (copy and paste, with minor modifications of this example configuration):
commands:
01_update_env:
command: "/tmp/update_environment_variables.sh"
files:
"/tmp/update_environment_variables.sh":
mode: "000755"
content : |
#!/bin/bash
RDS_HOSTNAME=$(/opt/elasticbeanstalk/bin/get-config environment -k RDS_HOSTNAME)
if [ -z "$RDS_HOSTNAME" ]; then
echo "Could not determine RDS hostname"
exit 1
fi
echo "RDS hostname $RDS_HOSTNAME..."
# Just export the variable at OS level, or make it visible to
# the rails env in some other way
export RDS_HOSTNAME=$RDS_HOSTNAME
# Process the rest of the variables...
# Probably we should create a list and iterate through it
A similar approach could be the one exposed in this stackoverrun question, but restricted to the container that Beanstalk will use to encapsulate your app. AFAIK, the container should receive as env variables the different RDS* ones corresponding to the database configuration.
Dan, be aware that I have not tested these solutions, they are only ideas: please, be careful with that, I do not want to cause any damage to your system.
I found an answer for this problem with a mysql server that might still help you. Basically, even though I followed all your steps, could see my envars using sudo /opt/elasticbeanstalk/bin/get-config environment and could connect directly to my database with the mysql command, I was still getting the following error:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) (Mysql2::Error::ConnectionError)
The solution turned out to be the fact that Elastic Beanstalk was not connecting my envars to my bundle exec rails console command in the eb ssh instance access. I solved the issue by prepending all of the required envars explicitly to any rails commands I ran from within the eb ssh instance access. So for example, in order to run rails console, I had to run the following:
RAILS_MASTER_KEY=xxxxxxx RAILS_ENV=production RDS_HOSTNAME=xxxxxxx RDS_PASSWORD=xxxxxxx RDS_USERNAME=xxxxxxx RDS_DB_NAME=xxxxxxx AWS_REGION=xxxxxxx AWS_BUCKET=xxxxxxx bundle exec rails c
Replace the xxxxxxxs above with the values from the corresponding variables in your EB > Configuration > Software tab, and you should be able to connect to the remote database and run migrations, rake tasks and other database-reliant functions.
For Linux2 instances I was having the same issue and just noticed that the env variables I set in the config just didn't exist for su that I had set myself to -- if I remain the default login after eb ssh env prints everything I expected
edit: sorry -- env printing of variables on linux 2 instance enabled by
https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-shell/
so what I did was find where those env variables were being exported for default user shell, which was /etc/profile.d/sh.local as noted in the above aws knowledge center link and just source that file when I needed to start the rails console as su

Can't connect to local MySQL server through socket in ubuntu while running rails

I trying to follow the steps here to run ruby on rails on linux, everything was fine except when i try to execute this
rake db:create
i got this error,
#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>"secretpassword", "host"=>"localhost", "database"=>"apps_development"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'apps_development'
#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>"secretpassword", "host"=>"localhost", "database"=>"apps_test"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'apps_test'
what this means?
The error is quite explicit:
#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
Your application can't connect to your MySQL database.
Your are trying to reach it through socket (/var/run/mysqld/mysqld.sock).
You have to know where Mysql socket is stored and adapt code OR MySQL config to match eachother.
Before that, you may want to check if Mysql is running, obviously you can not access to socket if Mysql is down. Check it with: sudo service mysql status.
If Mysql is up, check what following.
Find socket and check rights
Find socket path while Mysql running: mysql -e '\s;' | grep 'UNIX socket:'. You may need to add -u<USERNAME> -p<PASSWORD> depending your client configuration. It should result something like: UNIX socket: /var/run/mysqld/mysqld.sock. Here /var/run/mysqld/mysqld.sock is the socket location.
Check socket file: ls -l <SOCKET-PATH> (here: ls -l /var/run/mysqld/mysqld.sock).
If you got ls: cannot access /var/run/mysqld/mysqld.sock: No such file or directory then your socket file do not exists. Restarting Mysql may fix it (sudo service mysql restart).
If you got srwxrwxrwx 1 mysql mysql 0 nov. 25 10:07 /var/run/mysqld/mysqld.sock check rights and owners on this file (by default should be owned by mysql user & group and be executable at least to mysql user).
Configure your app
Now you know where socket file is and you are sure your app can access it, you may need to adapte your application to what you learn from first part.
According to Rails doc you must edit your config/database.yml file to set socket field to the socket location.

Putting Rails app into production - rake db:schema:load not working

I'm trying to deploy a new Rails app to a Bitnami/Ubuntu/Virtual Server. I am remote and using the SSH terminal.
I have successfully used Capistrano to cap deploy:update. My source is going to github and Capistrano is then putting it on the server.
So, I have this directory on the server:
/opt/bitnami/projects/ndeavor/releases/20130306180756
The server also has a PostgreSQL stack running. I have created my Postgresql user and empty database. I believe my next step is to run this command using the SSH console:
bitnami#linux:/opt/bitnami/projects/ndeavor/releases/20130306180756$ rake RAILS_ENV=production db:schema:load
Question 1 = Is that the correct next step?
When I run that command, I get this:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Questions 2 = How can I get Rake to find the PostgreSQL socket?
I could put something like this in the database.yml file:
socket: /var/pgsql_socket
But, I don't know what the correct entry should be
Thanks for your help!!
UPDATE1
I also tried having the database.yml file like this:
production:
adapter: postgresql
encoding: unicode
database: ndeavor_production
pool: 5
username: (user name)
password: (password)
socket: /var/run/postgresql
But, I get the same error:
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Why isn't it at least asking me for Unix domain socket "/var/run/postgresql" ??
UPDATE2
I found this:
"I have solved my problem by declaring the unix_socket_directory in postgresql.conf file to be /var/run/postgresql. It does seem for a standard build they should have a common location?
If you build from unmodified PG sources, the default socket location is
indeed /tmp. However, Debian and related distros feel that this
violates some distro standard or other, so they modify the source code
to make the default location /var/run/postgresql. So it depends on
whose build you're using."
But, I'm not sure if I should be changing the postgresql.conf file or the Rails database.yml file
UPDATE3
I looked in /var/run/postgresql directory and it's empty.
I can't find where the .s.PGSQL.5432 is located
As Bob noted, specifying host and port can fix the problem. Since he hasn't explained this in more detail I want to specify.
The default port is 5432, and the default "host" is a path to where it expects the socket. Port is always numeric, but host can be set for any libpq connection either to the network host or to the directory containing the socket. For example, connect using psql with
psql -h /tmp -p 5432 -U chris mydb
This will connect over the /tmp/.s.PGSQL.5432 socket. Now Ruby is somewhat different but the pg gem uses libpq so it should behave the same.
If you don't know where the socket is, the obvious next thing to try is the network address, and particularly localhost.

Resources