starting postgres server on Mac - macos

I am having this issue third or forth time where after restarting my Mac (Yosemite and same thing with previous OSX version) postgres server doesn't start. I had installed my postgres following this link. Any idea why this consistently occurs after every restart?
typing psql it gives
psql: 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"?

I just fixed this on my mac. I used homebrew to install postgres. These are the steps I took to get up and running again.
First check the status of the postgresql service:
pg_ctl -D /usr/local/var/postgres status
if you see this pg_ctl: no server running, unload the launch agent:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
then move your existing postgres data (move rather than delete - cheap insurance):
mv /usr/local/var/postgres /usr/local/var/postgres-deletemewhenitsallgood
then initialize your postgres database:
initdb /usr/local/var/postgres -E utf8
then load your launch agent:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
OR start it:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Now check the status again:
pg_ctl -D /usr/local/var/postgres status
if you see something like this, you are good to go.
pg_ctl: server is running (PID: 61295)
/usr/local/Cellar/postgresql/bin/postgres "-D" "/usr/local/var/postgres"
Then you can go into your postgres database:
psql -d postgres
Also after that you need to add postgress Role.
To Resolve missing postgres Role error

To start postgres server on mac:
install postgres with brew
brew install postgres
ensure you have permissions for /usr/local/postgres
sudo chown -R `whoami` /usr/local
then init the db:
initdb /usr/local/var/postgres
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
To load postgresql:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
To check your version:
psql --version
And to access postgres
psql -d postgres

Related

Installing PostgreSQL on Mac OS X

I am starting to use PostgreSQL on my Mac but I am stumped as to how I should go about it
I tried out instructions given in the below sites :
http://exponential.io/blog/2015/02/21/install-postgresql-on-mac-os-x-via-brew/
https://www.codementor.io/devops/tutorial/getting-started-postgresql-server-mac-osx
https://gist.github.com/sgnl/609557ebacd3378f3b72
but to little avail.
Here is my terminal log:-
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ brew install postgresql
Updating Homebrew...
Warning: postgresql 9.6.5 is already installed
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ postgres --version
postgres (PostgreSQL) 9.3.5
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ psql postgres
psql: 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"?
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ which postgresql
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ which postgres
/usr/local/php5/bin/postgres
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ postgres -D /usr/local/var/postgres
LOG: unrecognized configuration parameter "dynamic_shared_memory_type" in file "/usr/local/var/postgres/postgresql.conf" line 126
FATAL: configuration file "/usr/local/var/postgres/postgresql.conf" contains errors
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ postgres -D
/usr/local/php5/bin/postgres
postgres cannot access the server configuration file
"/usr/local/php5/bin/postgres/postgresql.conf": Not a directory
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ pg_ctl -D /usr/local/var/postgres start && brew services start postgresql
server starting
LOG: unrecognized configuration parameter "dynamic_shared_memory_type" in file "/usr/local/var/postgres/postgresql.conf" line 126
FATAL: configuration file "/usr/local/var/postgres/postgresql.conf" contains errors
Service `postgresql` already started, use `brew services restart
postgresql` to restart.
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$ postgres -V
postgres (PostgreSQL) 9.3.5
Pavan7Vasans-MacBook-Pro:~ pavan7vasan$
what has me in a fix is that despite the latest version of PostgreSQL being installed through homebrew, the current version is 9.3.5
Is this because of an older version of PostgreSQL present as shown through the which postgres command is conflicting?
Are all the above errors due to the older version still being the current version?
If possible, how do I change the version to the latest version?
Please do tell me what is to be done

Erros in installing PostgreSQL using homebrew

I'm using Mac OS X 10.8.5 and trying to install postgreSQL using homebrew. I've fixed several errors and warnings which have occurred when I ran brew info postgresql. However, I don't know how to fix the rest of the problems.
$brew info postgresql
postgresql: stable 9.3.2 (bottled)
http://www.postgresql.org/
Conflicts with: postgres-xc
/usr/local/Cellar/postgresql/9.3.2 (2924 files, 40M) *
Poured from bottle
From: https://github.com/Homebrew/homebrew/commits/master/Library/Formula/postgresql.rb
==> Dependencies
Required: readline ✔
Recommended: ossp-uuid ✔
==> Options
--32-bit
Build 32-bit only
--enable-dtrace
Build with DTrace support
--no-perl
Build without Perl support
--no-tcl
Build without Tcl support
--without-ossp-uuid
Build without ossp-uuid support
--without-python
Build without python support
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
https://github.com/Homebrew/homebrew/issues/issue/2510
To migrate existing data from a previous major version (pre-9.3) of PostgreSQL, see:
http://www.postgresql.org/docs/9.3/static/upgrading.html
When installing the postgres gem, including ARCHFLAGS is recommended:
ARCHFLAGS="-arch x86_64" gem install pg
To install gems without sudo, see the Homebrew wiki.
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres
When I run server, I get:
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `initialize': could not connect to server: No such file or directory (PG::ConnectionBad)
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I've uninstalled postgresql and reinstalled but seems like I wasn't able to remove the postgresql 8.x version.
I've read several different posts to install and run postgreSQL properly and tried what they've suggested but nothing seems to be working for me.
Installing PostgreSQL in Mac OS X El Capitan using homebrew
First check the 'owner' of the folder '/usr/local' by using this command:
$ ls -la '/usr'
If the owner of the file is 'root', run this command to change the owner to your currently logged user:
$ sudo chown -R `whoami` /usr/local
Install PostgreSQL using homebrew:
$ brew update
$ brew doctor
$ brew install postgresql#15
and that's it!
Uninstall PostgreSQL using homebrew:
$ brew uninstall postgres
then remove this folder: '/usr/local/var/postgres' and then change back the owner of your local
local folder to:
$ sudo chown -R root /usr/local
How to:
List brew services: $ brew services list
Run brew services: $ brew services start <service_name>
ex: $ brew services start postgresql
Stop brew serices: $ brew services stop <service_name>
Logging in to PostgreSQL for the first time:
1.) Run the postgres: $ brew services star postgresql
2.) Create a db using your username: $ createdb `whoami`
3.) Login to postgres: $ psql
To quit psql, just type: \q
4.) Create database (using terminal):
$ createdb <db_name>
psql tutorial:
http://blog.trackets.com/2013/08/19/postgresql-basics-by-example.html
http://exponential.io/blog/2015/02/21/install-postgresql-on-mac-os-x-via-brew/
Optional, installing pgAdmin:
1.) Download pdadmin: https://www.pgadmin.org/download/macosx.php
2.) Press ctrl+click then open to run the dmg file then drag it to your
Application folder. ctrl+click the pdadmin to run it so that it won't
ask for that annoying question again.
Login in to pgAdmin:
Note: Use your username and password when you installed your postgres for first login
then create a new user with privileges later or just change your password from
'File -> Change Password...'
Your Ruby code is apparently linked against the system version of PostgreSQL, which looks for a socket in /var/pgsql_socket. The Homebrew version would look in /tmp. (At this point, make sure that a file like /tmp/.s.PGSQL.5432 exists.) To work around this, you can tell your code to connect to the Homebrew version by specifying /tmp with the "host" option, however your code spells that.
In the future, make sure your Ruby gems are linked against the Homebrew version. Perhaps just uninstalling and reinstalling with the right paths set would do it.

Mismatching pg_dump on MacOS X

Locally, it appears that I have Postgresql 9.2.4 installed. I am guessing based off the last error message I received that I do not have the latest version:
pg_dump: server version: 9.3.1; pg_dump version: 9.2.4
pg_dump: aborting because of server version mismatch
On other posts here, what was told to do was to add the following the the .profile and then run the command:
export PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
. ~/.profile
which did not work.
2 weeks ago, I installed Postgresql with the simple command:
brew install postgresql
and that gave me the version I currently have. It appears as I don't have the latest version then.
How can I a) upgrade to 9.3.1 (which is what the server version looks to be) or b) do one of these fixes like the one I attempted above which did not work.
I am very new to Postgresql so a step by step would be very helpful for me.
brew upgrade postgresql
brew link --overwrite postgresql
Solved by adding this at the end of my ~/.bash_profile ( or ~/.zshrc for zsh)
export PG_DUMP="/Applications/Postgres.app/Contents/Versions/9.6/bin/"
PATH=$PG_DUMP:$PATH
Don't forget to run (for bash shell)
$ source ~/.bash_profile
or (for zsh)
$ source ~/.zshrc
You can check the path of pg_dump by running
$ which pg_dump
==> /Applications/Postgres.app/Contents/Versions/9.6/bin//pg_dump
MacOS Mojave (10.14.2)
Postgres.app (2.1.5)
PostgreSQL 9.6
There are upgrade instructions you have to follow between Postgresql 9.2 and 9.3...
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
mv /usr/local/var/postgres /usr/local/var/postgres92
brew update
brew upgrade postgresql
initdb /usr/local/var/postgres -E utf8
pg_upgrade -b /usr/local/Cellar/postgresql/9.2.4/bin -B /usr/local/Cellar/postgresql/9.3.1/bin -d /usr/local/var/postgres92 -D /usr/local/var/postgres
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

rails 3.2.2 (or 3.2.1) + Postgresql 9.1.3 + Ubuntu 11.10 Connection error

I am using PostgreSQL 9.1.3 (PostgreSQL 9.1.3 on x86_64-pc-linux-gnu, compiled by gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1, 64-bit) and rails either 3.2.2 or 3.2.1 on ubuntu 11.10.
Now, I can connect with below command with PostgreSQL
su postgres
enter password and I can see postgres=#
I am placing below details in my config/database.yml and executing "rails db" it is working fine.
development:
adapter: postgresql
encoding: utf8
reconnect: false
database: sample_app_db
pool: 5
username: postgres
password: passwordhere
host: localhost
I am using rvm to access my rails environment. but when I start server using "rails s" command and hit url with "http://localhost:3000", say - connection not establish.
try this way,
sudo -u postgres createuser -D -P your-current-ubuntu-username
and
sudo -u postgres createdb -O your-current-ubuntu-username your-database
open up this file /etc/postgresql/9.1/main/pg_hba.conf
change this line only:
local all all peer
to this:
local all all md5
Don't forget to restart the postgres server:
sudo service postgresql restart
Now check, with this command
psql -d your-database -U your-current-ubuntu-username -W
it should work
This solutions works for postgresql-9.1, here is the way to install
sudo apt-get install postgresql-9.1
I'd like to propose a slightly different approach which utilizes the file socket instead.
By allowing your Ubuntu user access to the database, everything should work without special connection parameters.
In the Ubuntu command line:
createuser -U postgres your-current-ubuntu-username
see the Manpage of createuser for details.
In your database.yml:
development:
adapter: postgresql
encoding: unicode
database: sample-app_development
pool: 5
username: your-current-ubuntu-username
password:

mongo - ruby connection problem

I have installed mongo and bson_ext
now I have created a .rb file with the following contents ::
require 'rubygems'
require 'mongo'
db = Mongo::Connection.new.db("mydb")
db = Mongo::Connection.new("localhost").db("mydb")
db = Mongo::Connection.new("localhost", 27017).db("mydb")
However I am getting following error on running the code
yuzaki#ubuntu:~$ ruby firstruby.rb
/home/ryuzaki/.rvm/gems/ruby-1.9.2-p136/gems/mongo-1.2.0/lib/mongo/connection.rb:451:in `connect': Failed to connect to a master node at localhost:27017 (Mongo::ConnectionFailure)
from /home/ryuzaki/.rvm/gems/ruby-1.9.2-p136/gems/mongo-1.2.0/lib/mongo/connection.rb:554:in `setup'
from /home/ryuzaki/.rvm/gems/ruby-1.9.2-p136/gems/mongo-1.2.0/lib/mongo/connection.rb:98:in `initialize'
from firstruby.rb:4:in `new'
from firstruby.rb:4:in `<main>'
Please help!
had this happen several times now, and here is the solution that works for me:
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo start mongodb
sudo status mongodb
This is definitely due to your mongo server not running. Since you're on Ubuntu, try doing a sudo /etc/init.d/mongodb start and then see if your code works.
I just encountered this due to my /etc/hosts file not containing an entry for "localhost" - consequently Ruby couldn't resolve "localhost". I suppose you can hardcode 127.0.0.1 into your code rather than "localhost" - or fix /etc/hosts to contain:
127.0.0.1 localhost
If you're on a Mac and used Brew, restarting the service solved it for me:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
You can find this info by running brew info mongodb.
On a mac, using brew I found that brew info mongodb and then using then
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents or mongod --config /usr/local/etc/mongod.conf worked!
Sometimes there is simply not enough space.
exception in initAndListen: 15926 Insufficient free space for
journals, terminating

Resources